update php multidimensional array - php

I want to update array based on pid and size s if product id and size both are matched whithin same array elements then it update the quantity by adding both the quantity and merge them in one index.Any help should be appreciated.
my code is:_
<?php
$details = array ( "0" => array ( "pid" => "402",
"q" => "1",
"s" => "0"
),
"1" => array ( "pid" => "403",
"q" => "2",
"s" => "0"
),
"2" => array ( "pid" => "402",
"q" => "3",
"s" => "0"
),
"3" => array ( "pid" => "403",
"q" => "1",
"s" => "0"
),
"4" => array ( "pid" => "405",
"q" => "1",
"s" => "0"
),
);
?>
Output Should look like
output :
0-pid 402 q 4 s 0
1-pid 403 q 3 s 0
4-pid 405 q 1 s 0

foreach($details as $p)
{
$output[$p["pid"]][$p["s"]]["q"]+=$p["q"];
}
Output
Array
(
[402] => Array
(
[0] => Array
(
[q] => 4
)
)
[403] => Array
(
[0] => Array
(
[q] => 3
)
)
[405] => Array
(
[0] => Array
(
[q] => 1
)
)
)
You can then display the array in any format you like

Related

Multi Dimension array to convert in string in php

Array ( [11] => Array ( [0] => A [1] => Attempt ) [ 12] => Array ( [0] => 0 [1] => None ) [ 13] => Array ( [0] => 0 [1] => None ) [ 14] => Array ( [0] => 0 [1] => None ) [ 15] => Array ( [0] => 0 [1] => None ) [ 16] => Array ( [0] => 0 [1] => None ) )
This is my array but i want in below STRING format:
11=>A=>Attempt,12=>0=>None,13=>0=>None,14=>0=>None,15=>0=>None,16=>0=>None
1. $keys=array_keys($total_answer)
2. for($i=0;$i<count($keys);$i++)
3. {
4. for($j=0;$j<count($total_answer[$keys[$i]]);$j++)
5. {
6. echo $total_answer[$keys[$i]][$j]
7. //Here I am getting confuse to make string
Thank You In Advance:)
One way to do it,
<?php
$array = array ( "11" => array ( "0" => "A" ,"1" => "Attempt" ) ,"12" => array ( "0" => 0, "1" => "None" ) ,"13" => array ( "0" => 0, "1" => "None" ) ,"14" => array ( "0" => 0 ,"1" => "None" ), "15" => array ( "0" => 0, "1" => "None" ), "16" => array ( "0" => 0, "1" => "None" ) );
foreach($array as $key=>$value){
$expected[] = $key.'=>'.$value[0].'=>'.$value[1];
}
echo implode(',',$expected);
?>
WORKING DEMO: https://3v4l.org/TDb0A
One foreach is enough
$out = [];
foreach ($total_answer as $k=>$v)
{
array_unshift($v, $k);
$out[] = implode('=>', $v);
}
echo implode(',', $out);

How to fetch record from an array if we have concept of array index, using array_column function?

I have an array of cryptocoin rates.
The array is looking as:
$array = Array ( [0] => Array ( [code] => BTC [name] => Bitcoin [rate] => 1 )
[1] => Array ( [code] => BCH [name] => Bitcoin Cash [rate] => 7.06364 )
[2] => Array ( [code] => USD [name] => US Dollar [rate] => 8185.84 ) )
I get results using $array[1]['rate'];
But i want to get result by [code] .
Like $array['USD']['rate']
Like $array['BCH']['rate']
How i can get the rate using currency code e.g USD
You can loop the array and build a new associative array to use in the rest of the project.
$array = Array( 0 => Array( "code" => "BTC", "name" => "Bitcoin", "rate" => 1 ),
"1" => Array ( "code" => "BCH", "name" => "Bitcoin Cash", "rate" => 7.06364 ),
"2" => Array ( "code" => "USD", "name" => "US Dollar", "rate" => 8185.84 ) );
foreach($array as $val){
$rates[$val["code"]] = $val;
}
echo $rates['USD']['rate']; // 8185.84
https://3v4l.org/0qs0n
Another option is to use array_column and array_combine to do it without loops.
$array = Array( 0 => Array( "code" => "BTC", "name" => "Bitcoin", "rate" => 1 ),
"1" => Array ( "code" => "BCH", "name" => "Bitcoin Cash", "rate" => 7.06364 ),
"2" => Array ( "code" => "USD", "name" => "US Dollar", "rate" => 8185.84 ) );
$keys = array_column($array, "code");
$rates = array_combine($keys, $array);
echo $rates['USD']['rate'];
You could use array_reduce and set the key to the value of code in the callback function:
$array = array_reduce($array, function($carry, $item) {
$carry[$item["code"]] = $item;
return $carry;
});
echo $array["USD"]["rate"]; //8185.84
Demo

how to get row index from mysql result in codeignitier when search by id or code

I have the data :
Array
(
[0] => Array
(
[id] => 12
[code] => 12345
[name] => Aaron
)
[1] => Array
(
[id] => 5
[code] => 16784
[name] => Bryan
)
[2] => Array
(
[id] => 35
[code] => 32467
[name] => Charlie
)
[3] => Array
(
[id] => 25
[code] => 44513
[name] => Denise
)
[4] => Array
(
[id] => 44
[code] => 15774
[name] => Michael
)
)
In my model i create function :
private function getPosition($field_search, $field_value){ // ID or code
$this->db->select('*');
$this->db->from($this->table);
$this->db->order_by('name','asc');
$result = $this->db->get()->result();
$pos = array_search($field_value, array_column($result, $field_search));
return ($pos !==false ? $pos : -1);
}
if i want to get row index of "32467", i just call
$pos = $this->getPosition('code', '32467');
then i got the row index is "2",
but i wonder, how to get row index if the query is :
$this->db->select('*');
$this->db->from($this->table);
$this->db->where('code', '32467');
$this->db->order_by('name','asc');
$result = $this->db->get()->result();
litte correction in my question,
i mean, if we index in database where code = "32467", we've got index is "2", how to get return if index is "2"
thanks for your help...
<?php
$userdb= Array
(
"0" => Array(
'id' => "12",
'code' => "12345",
'name' => 'Aaron'
),
"1" => Array
(
"id" => "5",
"code" => "16784",
"name" => "Bryan"
),
"2" => Array
(
"id" => "35",
"code" => "32467",
"name" => "Charlie"
),
"3" => Array
(
"id" => "25",
"code" => "44513",
"name" => "Denise"
),
"4" => Array
(
"id" => "44",
"code" => "15774",
"name" => "Michael"
),
);
$key = array_search(32467, array_column($userdb, 'code'));
echo ("The key is: ".$key);
private function getPosition($field_search, $field_value){ // ID or code
$this->db->select('*');
$this->db->from($this->table);
$this->db->order_by('name','asc');
$result = $this->db->get()->result();
$key = array_search($field_value, array_column($result, $field_search));
return $key;
}
?>

how to get facebook user work and education info and store in to variables or array php

i am facing problem in storing work and education information of Facebook user.
this is how i am getting info of user:
$profile_request=$fb->get('/me?fields=name,first_name,last_name,email,work,education');
and then convert the response in to multidimensional array:
$profile = $profile_request->getGraphNode()->asArray();
this is how i get the response:
$profile=Array
(
"name" => "abc pqr",
"first_name" => "abc",
"last_name" => "pqr",
"email" => "abc#gmail.com",
"work" => Array
(
"0" => Array
(
"employer" => Array
(
"id" => "348468651959125",
"name" => "Karachi university"
),
"location" => Array
(
"id" => "110713778953693",
"name" => "Karachi, Pakistan"
),
"position" => Array
(
"id" => "1556082398000870",
"name" => "Lecturer"
),
"start_date" => "2008-04-25"
),
"1" => Array
(
"description" => "i am teaching......",
"end_date" => "2011-06-20",
"employer" => Array
(
"id" => "486743441453589",
"name" => "Happy Palace Grammar School NORTH 3 - J2"
),
"location" => Array
(
"id" => "110713778953693",
"name" => "Karachi, Pakistan"
),
"position" => Array
(
"id" => "1556082398000870",
"name" => "Lecturer"
),
"start_date" => "2005-06-17"
)
),
"education" => Array
(
"0" => Array
(
"school" => Array
(
"id" => "193563427344550",
"name" => "Mehran Model Sec School"
),
"type" => "High School"
),
"1" => Array
(
"concentration" => Array
(
"0" => Array
(
"id" => "104076956295773",
"name" => "Computer Science"
)
),
"school" => Array
(
"id" => "113340788773685",
"name" => "Sir Adamjee Institute"
),
"type" => "College",
"year" => Array
(
"id" => "118118634930920",
"name" => "2012"
)
),
"2" => Array
(
"concentration" => Array
(
"0" => Array
(
"id" => "104076956295773",
"name" => "Computer Science"
)
),
"degree" => Array
(
"id" => "519451718218292",
"name" => "Bachelor of science in Computer Science in Computer Science"
),
"school" => Array
(
"id" => "107345492681211",
"name" => "SZABIST"
),
"type" => "Graduate School",
"year" => Array
(
"id" => "127342053975510",
"name" => "2016"
)
)
),
"id" => "145864899647476"
);
and this is my code:
$keys = array_keys($profile);
print_r($keys);
echo count($profile);
echo "<br>";
for ($i=4; $i <count($profile)-1 ; $i++) {
# code...
/*[4] => work [5] => education*/
echo $keys[$i]."<br>";
foreach ($profile[$keys[$i]] as $k => $v) {
# code...
echo "<br>$k<br>";
$keeys=array_keys($v);
print_r($keeys);
}
}

How to loop through a multi-layer array and replace some associate values in it?

How can I loop through a multi-layer array and replace some associate values in it?
For instance, this is my array,
$items = array(
0 => array(
"id" => "1",
"title" => "parent 1",
"children" => array()
),
1 => array(
"id" => "2",
"title" => "parent 2",
"children" => array (
0 => array(
"id" => "4",
"title" => "children 1",
"granchildren" => array(
0 => array(
"id" => "7",
"title" => "granchildren 1"
),
1 => array(
"id" => "8",
"title" => "granchildren 2"
)
)
),
1 => array(
"id" => "5",
"title" => "children 2",
"granchildren" => array()
)
),
),
3 => array(
"id" => "3",
"title" => "parent 3",
"children" => array()
)
);
These are two working functions I have,
function translate ($id){
$items = array(
0 => array(
"id" => 1,
"title" => "parent 1 en"
),
1 => array(
"id" => 4,
"title" => "children 1 en"
),
2 => array(
"id" => 8,
"title" => "granchildren 2 en"
)
);
foreach($items as $item) {
if($id === $item['id'])
{
return $item['title'];
}
}
}
function looper ($items){
$new_items = array();
foreach($items as $key => $item) {
if(isset($key) && is_array($key)){
$new_items[$key] = translate($item['id']);
}else {
//looper($item);
}
}
return $new_items;
}
print_r(looper ($items));
This is the result I am after,
Array
(
[0] => Array
(
[id] => 1
[title] => parent 1 en // translated
[children] => Array
(
)
)
[1] => Array
(
[id] => 2
[title] => parent 2
[children] => Array
(
[0] => Array
(
[id] => 4
[title] => children 1 en // translated
[granchildren] => Array
(
[0] => Array
(
[id] => 7
[title] => granchildren 1
)
[1] => Array
(
[id] => 8
[title] => granchildren 2 en // translated
)
)
)
[1] => Array
(
[id] => 5
[title] => children 2
[granchildren] => Array
(
)
)
)
)
[3] => Array
(
[id] => 3
[title] => parent 3
[children] => Array
(
)
)
)
Is it possible?
Sounds like a job for array_walk or array_walk_recursive.
It will call a user-supplied function for every item in an array. You can have it modify the array by reference to achieve what you're after.

Categories