Creating array from another array - php

I have an array that looks like this:
Array
(
[0] => Dog:110
[1] => Cat:111
[2] => Mouse:101
)
Based on that array, I want to create another array to look like this
Array
(
[Dog] => 110
[Cat] => 111
[Mouse] => 101
)
I know this is easy by creating my own function. But is there any way to do this with built in php function. Basically, I know I need to explode(), but is there any way to use this function in conjuction with one php's array functions or will I need to create my own function?

For fun one-liner:
parse_str(str_replace(':', '=', implode('&', $array)), $result);
print_r($result);

Use explode
$new_arr=array();
foreach($yourarr as $v)
{
$v = explode(':',$v);
$new_arr[$v[0]]=$v[1];
}
Demo

Related

multidimensional array to indexed array in php

i have this multidimensional array.
Array
(
[0] => Array
(
[car] => Toyota
)
[1] => Array
(
[car] => Ford
)
[2] => Array
(
[car] => Isuzu
)
[3] => Array
(
[car] => Chevrolet
)
)
i want to put them into indexed array like this..
Array = ("Toyota", "Ford", "Isuzu", "Chevrolet");
$result = array_map(function($item)
{ return $item['car']; }, $array);
Some functional approach. array_map
P.S. PHP has no indexed arrays
foreach($yourArray as $carArray)
{
$result[]=$carArray["car"];
}
And your understanding of indexed arrays is wrong. That example output you've shown contains all those values, not indexes, and since you didn't specify an index it will start from 0 and so on.
Option using array_map(), assuming your initial array is $arr
$new_arr = array_map(function($x){return $x['car'];}, $arr);
See demo
try using foreach and array_values, then save it into empty array. Hope it helps.

reconstruct an array to remove a parent in PHP

i want to restructure my array so that it looks better in a json
here is a print_r of my current variable:
Array
(
[0] => Array
(
[item_id] => 2
)
[1] => Array
(
[item_id] => 1
)
[2] => Array
(
[item_id] => 1
)
)
i want to reconstruct it be like this or similar:
EDIT
Array
(
[item_id] = array([0]=>'2',[1]=>'1', [2]=>'1');
)
sorry for my poor english m(_ _)m
i just want the item_id to have multiple values.
The hurdle
You actually can't in any way produce the output that you desire, since the key needs to be unique.
You can't use a key of item_id more than once, every time you try and set it, it will override what was in there last.
Think about it, how do you then look up the item with key of item_id, you can't, because three things would have that same key.
If the only reason is for cosmetics, I'd leave the output as you currently have it, although it may look a little messy in your JSON, it works.
A different approach
The best you can hope, is to get an output of:
'item_id' => array(
2,
1,
1
)
You can do this with the help of the array_map function:
$array = array('item_id' => array_map('current', $array));
This can be accomplished using this code.
$a['item_id'] = array();
foreach($arr as $key=>$val) {
$a['item_id'][] = $val['item_id'];
}
print_r($a);
$array = array('item_id' => array_map('current', $array));

PHP Convert multidimensional array to match format of another

I have two arrays, one is generated by using explode() on a comma separated string and the other is generated from result_array() in Codeigniter.
The results when doing print_r are:
From explode():
Array
(
[0] => keyword
[1] => test
)
From database:
Array
(
[0] => Array
(
[name] => keyword
)
[1] => Array
(
[name] => test
)
)
I need them to match up so I can use array_diff(), what's the best way to get them to match? Is there something other than result_array() in CI to get a compatible array?
You could create a new array like this:
foreach($fromDatabase as $x)
{
$arr[] = $x['name'];
}
Now, you will have two one dim arrays and you can run array_dif.
$new_array = array();
foreach ($array1 as $line) {
$new_array[] = array('name' => $line);
}
print_r($new_array);
That should work for you.

Concatenate the elements in a PHP array

How can I turn the following array below so it looks like example 2 using PHP.
Example 1 array.
Array ( [0] => &sub1=a1 [1] => &sub2=aa [2] => &sub3=glass-and-mosaics [3] => &sub4=guides-and-reviews [4] => &sub5=silent-movies [5] => &sub6=even-more-eastern-religions-and-philosophies )
Example 2.
&sub1=a1&sub2=aa&sub3=glass-and-mosaics&sub4=guides-and-reviews&sub5=silent-movies&sub6=even-more-eastern-religions-and-philosophies
can use implode
You can use the implode function.
$arr = array(0 => '&sub1=a1',1 => '&sub2=aa');
$str = implode('',$arr);
just do a $myVar = implode('', $array);
If this is a query fragment of a URL, use http_build_query instead:
echo http_build_query(
'sub1'=>'a1',
'sub2'=>'aa',
'sub3'=>'glass-and-mosaics',
'sub4'=>'guides-and-reviews',
'sub5'=>'silent-movies',
'sub6'=>'even-more-eastern-religions-and-philosophies'
);

Filter an array

I have an array like this:
$categories_array = array(
[0] => 'category_1',
[1] => 'category_2',
[2] => 'category_3',
[3] => 'category_4'
)
I'd like to "filter" the array to get a new one. For example, I'd like to have a new array with only 'category_2' and 'category_3' like this:
$new_categories_array = array(
[1] => 'category_2',
[2] => 'category_3',
)
How can I accomplish this result?
unset($new_categories_array[0]);
unset($new_categories_array[3]);
..might do the trick
See
array_diff — Computes the difference of arrays
array_intersect — Computes the intersection of arrays
Example:
$original = array('category_1','category_2','category_3','category_4');
$new = array_diff($original, array('category_1', 'category_4'));
print_r($new);
Output:
Array
(
[1] => category_2
[2] => category_3
)
When using array_intersect the returned array would contain cat 1 and 4 obviously.
Use preg_grep:
$new_categories_array = preg_grep('/category_[23]/', $categories_array);
While I agree preg_grep is a good solution in your example, if you want a more general case function, look at array_filter - http://ca.php.net/manual/en/function.array-filter.php

Categories