Convert 2d array by specified 2d format - php

I need to convert the below 2d array in to specified 2d array format. Array contains multiple parent and multiple child array. Also, have tried to convert the code, but am not getting the expected output.
This is the code what i have tried,
$a1 = array(
'0' =>
array(
'banner_details' =>
array(
'id' => 2,
'section_id' => 24
),
'slide_details' =>
array(
0 => array(
'id' => 74,
'name' => 'Ads1'
),
1 => array(
'id' => 2,
'name' => 'Ads2'
)
)
),
'1' =>
array(
'banner_details' =>
array(
'id' => 106,
'section_id' => 92
),
'slide_details' =>
array(
0 => array(
'id' => 2001,
'name' => 'Adv1'
),
1 => array(
'id' => 2002,
'name' => 'Adv2'
)
)
)
);
$s = [];
for($i = 0; $i<2; $i++) {
foreach($a1[$i]['slide_details'] as $vs){
$s[] = $vs;
}
}
My output:
Array
(
[0] => Array
(
[id] => 74
[name] => Ads1
)
[1] => Array
(
[id] => 2
[name] => Ads2
)
[2] => Array
(
[id] => 2001
[name] => Adv1
)
[3] => Array
(
[id] => 2002
[name] => Adv2
)
)
Expected output:
Array
(
[24] => Array
(
[0] => 74
[1] => 2
)
[92] => Array
(
[0] => 2001
[1] => 2002
)
)
please check the above code and let me know.
Thanks,

You can apply next simple foreach loop with help of isset() function:
foreach($a1 as $data){
if (isset($data['banner_details']['section_id'])){
$s[$data['banner_details']['section_id']] = [];
if (isset($data['slide_details'])){
foreach($data['slide_details'] as $row){
$s[$data['banner_details']['section_id']][] = $row['id'];
}
}
}
}
Demo
If you know that indexes like banner_details or slide_details or section_id will be there always then you can skip isset() in if statements.

You can use array_column function for simple solution:
$result = [];
foreach ($a1 as $item)
{
$result[$item['banner_details']['section_id']] = array_column($item['slide_details'], 'id');
}
var_dump($result);

Related

How to get Php multidimensional array same key’s same value’s related total in new array?

Php multidimensional array same key’s same value’s related total in
new array. I have an array of following mentioned. i need new array
as total qty of same item_id. anyone can help would be appreciate.
My Original Array is as following
Array
(
[a] => Array
(
[item] => Array
(
[item_id] => 1
)
[qty] => 0
),
[b] => Array
(
[item] => Array
(
[item_id] => 2
)
[qty] => 35
),
[c] => Array
(
[item] => Array
(
[item_id] => 2
)
[qty] => 15
),
[e] => Array
(
[item] => Array
(
[item_id] => 3
)
[qty] => 20
),
);
I want array Output like following :
Array(
[0] => Array (
[item_id] => 1,
[item_total_qty] => 0,
)
[1] => Array (
[item_id] => 2,
[item_total_qty] => 50,
)
[2] => Array (
[item_id] => 3,
[item_total_qty] => 20,
)
);
Hope it help
$arrays = array(
'a' => array(
'item' => array(
'item_id' => 1
),
'qty' => 0
),
'b' => array(
'item' => array(
'item_id' => 2
),
'qty' => 35
),
'c' => array(
'item' => array(
'item_id' => 2
),
'qty' => 15
),
'd' => array(
'item' => array(
'item_id' => 3
),
'qty' => 20
)
);
$result = array();
foreach ($arrays as $key => $array) {
if (is_array($result) && !empty($result)) {
foreach ($result as $key => $r) {
if ($r['item_id'] == $array['item']['item_id']) {
$result[$key]['item_total_qty'] += $array['qty'];
continue 2;
}
}
$result[] = array(
'item_id' => $array['item']['item_id'],
'item_total_qty' => $array['qty']);
} else {
$result[] = array(
'item_id' => $array['item']['item_id'],
'item_total_qty' => $array['qty']);
}
}
Simple foreach on your original table:
$sorted = array();
foreach ($original as $item) {
$id = $item['item']['item_id'];
$sorted[$id]['item_total_qty'] = $sorted[$id] ? $sorted[$id] + $item['qty'] : item['qty'];
$sorted[$id]['item_id'] = $id;
}
$sorted = array_values($sorted);

foreach loop is not working after print array

I've tried to loop of following array.
Array
(
[mech_info] => Array
(
[make] => Amaka
[0] => Array
(
[year] => 2001
[model] => Array
(
[0] => Test one
[1] => test fix
[2] => Hamour
[3] => Imagica
)
)
[1] => Array
(
[year] => 2002
[model] => Array
(
[0] => Test Two
)
)
[2] => Array
(
[year] => 2014
[model] => Array
(
[0] => Test three
)
)
[3] => Array
(
[year] => 2015
[model] => Array
(
[0] => test four
)
)
)
)
Array
(
[mech_info] => Array
(
[make] => PRI
[0] => Array
(
[year] => 2005
[model] => Array
(
[0] => PRIMODE
[1] => Temp Pri
[2] => primode
[3] => yyy
)
)
)
)
I want to do it with foreach loop. I have tried by following code but it is not show anything except
`print_r($_POST['mech_show']);`.
$all_make_model= $_POST['mech_show'];
$all_make_model_data = $all_make_model['mech_info'];
foreach ($all_make_model_data as $key => $mec_value) {
echo "string";
echo $meta_value['make'];
}
echo "<pre>";
print_r($_POST['mech_show']);
exit();
also not able to go under foreach and data not print in loop.
given me error
Notice: Undefined index: mech_info
Warning: Invalid argument supplied for foreach() in
i also trie this way but
$all_make_model= $_POST['mech_show'];
$all_make_model_data = $_POST['mech_info'];
foreach ($all_make_model as $key => $mec_value) {
echo "<pre>";
print_r($mec_value['make']);
echo "</pre>";
}
echo "<pre>";
print_r($all_make_model['mech_info']);
but it's showing Warning: Illegal string offset 'mech_info' in ..
I don't know if my code is wrong or I'm missing something anyone pls help me.
Thank You
Some change your foreach loop. it $meta_value['make'] should be $mec_value['make']
So,
$all_make_model= $_POST['mech_show'];
//$all_make_model_data = $all_make_model['mech_info'];
foreach ($all_make_model as $key => $mec_value) {
echo "<pre>";
print_r($mec_value['make']);
echo "</pre>";
}
try
$all_array=array("mech_info"=>array("make"=>"Amaka",array("year"=>2001,"model"=>array("one","two","three")),array("year"=>2002,"model"=>array("one","two","three")),array("year"=>2003,"model"=>array("one","two","three")),array("year"=>2004,"model"=>array("one","two","three"))),array("mech_info"=>array("make"=>"PRI",array("year"=>2001,"model"=>array("one","two","three")))));
$all_make_model= $all_array;
//$all_make_model_data = $all_make_model['mech_info'];
//print_r($all_make_model['mech_info']);
foreach ($all_make_model['mech_info'] as $key => $mec_value) {
if(is_numeric($key)) continue;
echo $mec_value; // output Amaka
}
exit();
This Code just work.
To iterate on multiple mech_info, i added a leave in the array, because otherwise you are tring to create multiple object with the same index.
$p = Array('mech_show' => Array(
0 => Array(
'mech_info' => Array(
'make' => 'Amaka',
'0' => Array(
'year' => 2001,
'model' => Array(
0 => 'Test one',
1 => 'test fix',
2 => 'Hamour',
3 => 'Imagica'
)
),
'1' => Array(
'year' => 2002,
'model' => Array(
0 => 'Test Two'
)
),
'2' => Array(
'year' => 2014,
'model' => Array(
0 => 'Test three'
)
),
'3' => Array(
'year' => 2015,
'model' => Array
(
0 => 'test four'
)
)
)
),
1=>Array(
'mech_info' => Array(
'make' => 'PRI',
'0' => Array(
'year' => 2005,
'model' => Array(
0 => 'PRIMODE',
1 => 'Temp Pri',
2 => 'primode',
3 => 'yyy'
)
)
)
)
)
);
$all_make_model= $p['mech_show'];
foreach($all_make_model as $all_make_model_data){
foreach($all_make_model_data as $mech_info)
var_dump($mech_info['make']);
}
where you have to replace $p with $_POST

Codeigniter XML-RPC Response Multi Dimensional Array Approach

I need to create an XML-RPC server that gets cities with their corresponding IDs. What I do as a response is looking weird to me because of unnecessary duplicate entries but I couldnt find a better way.
Array
(
[cityID] => Array
(
[0] => 34
[1] => 35
[2] => 06
)
[cityName] => Array
(
[0] => Istanbul
[1] => Izmir
[2] => Ankara
)
)
I implemented above response. With this implementation:
$response = array(
array(
'cityID' => array(array('34', '35', '06'), 'array'),
'cityName' => array(array('Istanbul', 'Izmir', 'Ankara'), 'array')
),
'struct'
);
The problem is I want to take a response like this :
Array
(
[cities] => Array
(
['34'] => 'Istanbul'
['35'] => 'Izmir'
['06'] => 'Ankara'
)
)
So I tried to implement it like this :
$response = array(
array(
'cities' => array(array('34'=>'Istanbul', '35'=>'Izmir', '06'=>'Ankara'), 'array')
),
'struct'
);
But it fails with this implementation. What am I doing wrong ?
Thanks
You have array like following
$response = array ( 'cityID' => array (
0 => 34,
1 => 35,
2 => 06
),
'cityName' => array(
0 => 'Istanbul',
1 => 'Izmir',
2 => 'Ankara'
)
);
$newarray = array();
foreach($response['cityID'] as $key => $cityid){
$newarray['cities'][$cityid] = $response['cityName'][$key];
}
print_r($newarray);
You will be getting the expected array.
Array
(
[cities] => Array
(
[34] => Istanbul
[35] => Izmir
[6] => Ankara
)
)
This is how I do it, in Code Igniter 3
$array = array ( 'cityID' => array (
0 => 34,
1 => 35,
2 => 06
),
'cityName' => array(
0 => 'Istanbul',
1 => 'Izmir',
2 => 'Ankara'
)
);
foreach($array['cityID'] as $key => $cityid){
$response[] = array(array(
$cityid => array($array['cityName'][$key],'string'),
),'struct');
}
return $this->xmlrpc->send_response(array($response,'array'));

PHP Replace Array Values

I have 2 multidimensional arrays that I am working with:
$arr1 =
Array
([type] => characters
[version] => 5.6.7.8
[data] => Array
([Char1] => Array
([id] => 1
[name] =>Char1
[title] =>Example
[tags] => Array
([0] => DPS
[1] => Support))
[Char2] => Array
([id] => 2
[name] =>Char2
[title] =>Example
[tags] => Array
([0] => Tank
[1] => N/A)
)
)
etc...
$arr2=
Array
([games] => Array
([gameId] => 123
[gameType => Match
[char_id] => 1
[stats] => Array
([damage] => 55555
[kills] => 5)
)
([gameId] => 157
[gameType => Match
[char_id] => 2
[stats] => Array
([damage] => 12642
[kills] => 9)
)
etc...
Basically, I need almost all the data in $arr2... but only the Char name from $arr1. How could I merge or add the $arr1['name'] key=>value into $arr2 where $arr1['id'] is equal to $arr2['char_id'] as the "id" field of each array is the same number.
I've attempted using array_merge and array_replace, but I haven't come up with any working solutions. This is also all data that I am receiving from a 3rd party, so I have no control on initial array setup.
Thanks for any help or suggestions!
Actually, this is quite straighforward. (I don't think there a built-in function that does this.)
Loop $arr2 and under it loop also $arr1. While under loop, just add a condition that if both ID's match, add that particular name to $arr2. (And use some referencing & on $arr2)
Consider this example:
// your data
$arr1 = array(
'type' => 'characters',
'version' => '5.6.7.8',
'data' => array(
'Char1' => array(
'id' => 1,
'name' => 'Char1',
'title' => 'Example',
'tags' => array('DPS', 'Support'),
),
'Char2' => array(
'id' => 2,
'name' => 'Char2',
'title' => 'Example',
'tags' => array('Tank', 'N/A'),
),
),
);
$arr2 = array(
'games' => array(
array(
'gameId' => 123,
'gameType' => 'Match',
'char_id' => 1,
'stats' => array('damage' => 55555, 'kills' => 5),
),
array(
'gameId' => 157,
'gameType' => 'Match',
'char_id' => 2,
'stats' => array('damage' => 12642, 'kills' => 9),
),
),
);
foreach($arr2['games'] as &$value) {
$arr2_char_id = $value['char_id'];
// loop and check against the $arr1
foreach($arr1['data'] as $element) {
if($arr2_char_id == $element['id']) {
$value['name'] = $element['name'];
}
}
}
echo '<pre>';
print_r($arr2);
$arr2 should look now like this:
Array
(
[games] => Array
(
[0] => Array
(
[gameId] => 123
[gameType] => Match
[char_id] => 1
[stats] => Array
(
[damage] => 55555
[kills] => 5
)
[name] => Char1 // <-- name
)
[1] => Array
(
[gameId] => 157
[gameType] => Match
[char_id] => 2
[stats] => Array
(
[damage] => 12642
[kills] => 9
)
[name] => Char2 // <-- name
)
)
)
Iterate over $arr2 and add the data to it from the matching $arr1 array value:
$i = 0;
foreach($arr2['games'] as $arr2Game){
$id = $arr2Game['char_id'];
$arr2['games'][$i]['name'] = $arr1['data'][$id]['name'];
$i++;
}
Have not tested this code.
If I'm understanding you correctly, you want to add a name index to each of the arrays within the $arr2['games'] array.
foreach($arr2['games'] as $key => $innerArray)
{
$arr2['games'][$key]['name'] = $arr1['data']['Char'.$innerArray['char_id']]['name'];
}

Get multiple arrays from multidimensional array based on common IDs

I have one large array and I want to group this into other arrays based on a common ID so that I can use array_splice to get only the first and last occurrence of that ID.
array(
[0] => array(id => 34, name = "walter"),
[1] => array(id => 25, name = "walter jr"),
[2] => array(id => 34, name = "saul"),
[3] => array(id => 25, name = "jesse"),
[4] => array(id => 25, name = "todd")
)
What I want to end up with is something like this.
array(
[0] => array(
id => 34, name = "walter",
id => 34, name = "saul"
),
[1] => array(
id => 25, name = "walter jr",
id => 25, name = "jesse",
id => 25, name = "todd"
)
)
I'm having a really hard time trying to wrap my head around how to accomplish this and have searched all over. I've found some solutions using array_unique and array_diff but i'm never able to get the result i'm looking for.
You can use array_reduce to group array elements, see below:
$data = array(
0 => array('id' => 34, 'name' => "walter"),
1 => array('id' => 25, 'name' => "walter jr"),
2 => array('id' => 34, 'name' => "saul"),
3 => array('id' => 25, 'name' => "jesse"),
4 => array('id' => 25, 'name' => "todd")
);
$result = array_reduce($data, function ($result, $item){
if (!isset($result[$item['id']])) {
$result[$item['id']] = array();
}
$result[$item['id']][] = $item;
return $result;
}, array());
print_r(array_values($result));
and result is:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 34
[name] => walter
)
[1] => Array
(
[id] => 34
[name] => saul
)
)
[1] => Array
(
[0] => Array
(
[id] => 25
[name] => walter jr
)
[1] => Array
(
[id] => 25
[name] => jesse
)
[2] => Array
(
[id] => 25
[name] => todd
)
)
)
If you want the key 0,1,2,3,4 ... just go through the entire array and overwrite the keys.
But depending on what you are doing, use foreach to traverse the array without the key. =p
$all_array = array(
0 => array( 'id' => 34 , 'name' => "walter" ) ,
1 => array( 'id' => 25 , 'name' => "walter jr" ) ,
2 => array( 'id' => 34 , 'name' => "saul" ) ,
3 => array( 'id' => 25 , 'name' => "jesse" ) ,
4 => array( 'id' => 25 , 'name' => "todd" )
) ;
$array_sort = array() ;
foreach ( $all_array as $piece_array ) {
$array_sort[$piece_array['id']][] = $piece_array ;
}
ksort( $array_sort ) ;
var_dump( $array_sort ) ;

Categories