End Your If

Helping you solve those tough coding problems!

Learn some great development tips and techniques through recipes that clearly define the problem being solved with a working solution.
Popular Tags: cakephp | theory | optimization | javascript-development | asp-net | ajax | sql | php-development | jquery-development | css

Search:
Follow @endyourif rss Facebook LinkedIn
My Books

JavaScript - Remove a specific element from an array JavaScript - Remove a specific element from an array

Published on Feb 7, 2019 by Jamie Munro

It's quite often that I have an array of elements and when a certain condition happens that I need to remove a particular element from my JavaScript array.

Removing an element from an array is a two part process:

  1. Find the element in the array

  2. Remove the element at the position found


Let's take a look at an example:

Read more >

Tags: array | JavaScript | splice | indexof

Re-map key/value array data in PHP Re-map key/value array data in PHP

Published on Mar 19, 2009 by Jamie Munro

I was recently tasked with a situation where I needed to populate about 10 different "Settings" for every user in the current database.

This example is specifically for CakePHP, however, it could easily be used elsewhere.  My goal was the following, I had an array that was key value paired as follows:

array(
 [0] => array(
  'User' => array(
   'id' => 1)
  ),
 [1] => array(
  'User' => array(
   'id' => 2)
  ),
 [2] => array(
  'User' => array(
   'id' => 3)
  ),
 [3] => array(
  'User' => array(
   'id' => 4)
  ),
)

The result I needed was as follows:

array(
 [0] => array(
  'UserSetting' => array(
   'user_id' => 1)
  ),
 [1] => array(
  'UserSetting' => array(
   'user_id' => 2)
  ),
 [2] => array(
  'UserSetting' => array(
   'user_id' => 3)
  ),
 [3] => array(
  'UserSetting' => array(
   'user_id' => 4)
  ),
)

With a few simple lines of code, I was able to quickly and easily achieve this, let me show you how.

Read more >

Tags: CakePHP | array | key/value | PHP | CakePHP