Change the key in a PHP array - php

I want to change the key of my array in php.
Here an exemple :
array (size=5)
0 =>
array (size=2)
'iden' => string '01' (length=8)
'don' => string '17' (length=2)
1 =>
array (size=2)
'iden' => string '02' (length=8)
'don' => string '17' (length=2)
2 =>
array (size=2)
'iden' => string '03' (length=8)
'don' => string '17' (length=2)
And I want to change my array like this :
array (size=5)
0 =>
array (size=2)
0 => string '01' (length=8)
1 => string '17' (length=2)
1 =>
array (size=2)
0 => string '02' (length=8)
1 => string '17' (length=2)
2 =>
array (size=2)
0 => string '03' (length=8)
1 => string '17' (length=2)
Thanks in advance

You can use the array_values function to remove named keys:
foreach($array as &$item) {
$item = array_values($item);
}
unset($item); // Remove reference
Note the & in the foreach. This creates a reference in the $item variable to the corresponding array element which means you can edit it in your loop.
If you want, you can also write this in a single line using array_map:
$array = array_map("array_values", $array);

Related

Remove array 0 in array PHP

I have 2 product id and my code is:
$Final=array();
foreach ($ids as $me)
{
$op=DB::table('product')->where(['id'=>$me])->get();
//$Final[]= $op;
array_push($Final,$op);
}
This code returns:
array (size=1)
0 =>
array (size=1)
0 =>
array (size=15)
'id' => string '34' (length=2)
'title' => string 'گوسفند' (length=12)
'title_url' => string 'sheep' (length=5)
'code' => string 'eerer' (length=5)
'code_url' => string 'eerer' (length=5)
'content' => string '<p>sheep</p>
' (length=14)
'cat' => string '68' (length=2)
'price' => string '50000' (length=5)
'product_state' => string '1' (length=1)
'date' => string '' (length=0)
'order_number' => string '0' (length=1)
'Special' => string '0' (length=1)
'View' => string '0' (length=1)
'number_product' => string '1' (length=1)
'discounts' => string '' (length=0)
I need to remove
array (size=2) 0 => array (size=1) 0 =>
$ids => filter id
for get product number for example (22,34)
I Think you should try this.
$Final=array();
foreach ($ids as $me){
$op=DB::table('product')->where(['id'=>$me])->get();
if($op) {
array_push($Final,$op[0]);
}
}
Then you will get these values.
array (size=2)
0 =>
array (size=15)
'id' => string '34' (length=2)
1 =>
array (size=15)
'id' => string '22' (length=2)
If you are using Any framework then framwork provide us some methods to run query with where in to get all the records in single query.
$op=DB::table('product')->whereIn('id'=>$ids)->get();
you will get array of collection for all the products.

Compare arrays and add missing key value to array [duplicate]

This question already has answers here:
PHP, Merging arrays with common keys
(2 answers)
Closed 6 years ago.
I have two arrays:
First
array (size=6)
0 =>
array (size=2)
'Age' => string '25-34' (length=5)
'Count' => string '45' (length=2)
1 =>
array (size=2)
'Age' => string '55-64' (length=5)
'Count' => string '1' (length=1)
2 =>
array (size=2)
'Age' => string '13-17' (length=5)
'Count' => string '3' (length=1)
3 =>
array (size=2)
'Age' => string '35-44' (length=5)
'Count' => string '11' (length=2)
4 =>
array (size=2)
'Age' => string '18-24' (length=5)
'Count' => string '46' (length=2)
5 =>
array (size=2)
'Age' => string '45-54' (length=5)
'Count' => string '2' (length=1)
Second:
array (size=5)
0 =>
array (size=2)
'Age' => string '65+' (length=3)
'Count' => string '1' (length=1)
1 =>
array (size=2)
'Age' => string '13-17' (length=5)
'Count' => string '4' (length=1)
2 =>
array (size=2)
'Age' => string '35-44' (length=5)
'Count' => string '3' (length=1)
3 =>
array (size=2)
'Age' => string '25-34' (length=5)
'Count' => string '11' (length=2)
4 =>
array (size=2)
'Age' => string '18-24' (length=5)
'Count' => string '20 |' (length=4)
Now what here I am getting is that First array size is larger than Second one, so I need a solution for making the small size array similar to larger size array.
With same keys, and add value zero to new added keys value.
array_merge( $firstArray, $secondArray );
Please see array_merge();

PHP array index to array associative

I got a array of array in PHP that look like this :
array (size=3)
0 =>
array (size=3)
0 => string 'abc' (length=3)
1 => string 'def' (length=3)
2 => string 'ghi' (length=3)
1 =>
array (size=3)
0 => string '01234' (length=5)
1 => string '01234' (length=5)
2 => string '01234' (length=5)
2 =>
array (size=3)
0 => string '98765' (length=5)
1 => string '98765' (length=5)
2 => string '98765' (length=5)
Now I want the first array to be the key of a assosiative array for the rest of the parent array, or kind :
array (size=2)
0 =>
array (size=3)
'abc' => string '01234' (length=5)
'def' => string '01234' (length=5)
'ghi' => string '01234' (length=5)
1 =>
array (size=3)
'abc' => string '98765' (length=5)
'def' => string '98765' (length=5)
'ghi' => string '98765' (length=5)
EDIT: But I can only get the first array like this to define the header :
$header = reset($tabOfTabs);
You can try this -
$indexes = array_shift($your_array); // pop out the first array to set the indexes
foreach($your_array as $key => $array) {
$your_array[$key] = array_combine($indexes, $array); // combine the keys & sub-arrays
}
Demo

adding string keys to inner arrays

I wish to add string keys to my inner PHP arrays. So, I want to convert this:
array (size=2)
0 => array (size=3)
0 => string 'X705' (length=4)
1 => string 'X723' (length=4)
2 => string 'Sue' (length=0)
1 => array (size=3)
0 => string 'X714' (length=4)
1 => string 'X721' (length=4)
2 => string 'John' (length=0)
to this:
array (size=2)
0 =>
array (size=3)
'code1' => string 'X705' (length=4)
'code2' => string 'X723' (length=4)
'name' => string 'Sue' (length=0)
1 =>
array (size=3)
'code1' => string 'X714' (length=4)
'code2' => string 'X721' (length=4)
'name' => string 'John' (length=0)
I think I need to use array_walk but cannot fathom it out. Any help appreciated.
You can use array_map for that purpose:
$newarray = array_map(function($x) {
return array("code1" => $x[0], "code2" => $x[1], "name" => $x[2]);
}, $array);
where $array is your input array.
Start with this:
foreach ($array as $key=>$item) {
$item['code1']=$item[0];
unset($item[0]);
$item['code2']=$item[1];
unset($item[1]);
$item['name']=$item[2];
unset($item[2]);
$array[$key]=$item;
}
I would use array_map() but here's an alternate:
foreach($array as &$v) {
$v = array_combine(array('code1','code2','name'), $v);
}

Tri-mensional array looping

I'm really stuck at a certain point in my project. I'm trying to convert a two-dimensional array into a three-dimensional array. Currently I´m at this point:
array (size=4)
0 =>
array (size=4)
0 => string 'foo' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '0' (length=1)
1 =>
array (size=4)
0 => string 'bar' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '1' (length=1)
2 =>
array (size=4)
0 => string 'bas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '1' (length=1)
3 =>
array (size=4)
0 => string 'tas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '0' (length=1)
What I would like to do is make a new array with key values of array[ ][3] (I hope this makes any sense) so something like this:
array (size=2)
0 => array (size=2)
0 => array (size=2)
0 => string 'foo' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
1 => array (size=2)
0 => string 'tas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
1 => array (size=2)
0 => array (size=2)
0 => string 'bar' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
1 => array (size=2)
0 => string 'bas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
Right now this is the best way I can explain it, I'm not sure if I make any sense or if this is even possible, but if anyone could shed a light on this it would be much appreciated!
Thanks in advance,
Thomas
$array = array();
$array[] = array('foo','foo','foo',1);
$array[] = array('a','a','a',1);
$array[] = array('b','a','a',0);
$array[] = array('c','a','a',0);
$new_array =group_by_sub_array_key($array,3);
print_r($new_array);
function group_by_sub_array_key($array,$kn){
$new_array = array();
foreach($array AS $v){
$key=$v[$kn];
if(!array_key_exists($v[$kn],$new_array)){
$new_array[$key]=array();
}
unset($v[$kn]);
$new_array[$key][] = $v;
}
return $new_array;
}

Categories