How to combine one array in php - php

How to my array combine :
$visitor_tracvking_data = json_encode($array);
print_r($visitor_tracvking_data);
Output print_r this:
array (size=6)
0 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'direct' => string '261' (length=3)
1 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'social_media' => string '3' (length=1)
2 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'search' => string '3' (length=1)
3 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'chrome' => string '168' (length=3)
4 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'firefox' => string '68' (length=2)
5 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'netscape' => string '31' (length=2)
How to out put this type:-
array (size=2)
0 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'direct' => string '261' (length=3)
'social_media' => string '3' (length=1)
'search' => string '3' (length=1)
1 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'chrome' => string '168' (length=3)
'firefox' => string '68' (length=2)
'netscape' => string '31' (length=2)

Just loop through the array and generate a new array with tracktitle key -
$new = array();
foreach($array as $key_top => $value) {
foreach($value as $key => $val) {
$new[$value['tracktitle']][$key] = $val;
}
}
return $new;
OR for json encoded string -
return json_encode($new);

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.

php replace values of array elements based on the lookup from another array

I have an array $fruits which is as following:
array (size=4)
0 =>
array (size=2)
'fruit' => string 'banana' (length=5)
'origin' => string 'cat1' (length=4)
1 =>
array (size=2)
'fruit' => string 'orange' (length=6)
'origin' => string 'cat2' (length=4)
2 =>
array (size=2)
'fruit' => string 'apple' (length=5)
'origin' => string 'cat3' (length=4)
3 =>
array (size=2)
'fruit' => string 'lemon' (length=5)
'origin' => string 'cat4' (length=4)
and I have another array $origins which has the old origins and new origins of the fruits:
array (size=5)
0 =>
array (size=2)
'oldOrigin' => string 'cat1' (length=5)
'newOrigin' => string 'new1' (length=4)
1 =>
array (size=2)
'oldOrigin' => string 'cat2' (length=5)
'newOrigin' => string 'new2' (length=4)
2 =>
array (size=2)
'oldOrigin' => string 'cat3' (length=5)
'newOrigin' => string 'new3' (length=4)
3 =>
array (size=2)
'oldOrigin' => string 'cat4' (length=5)
'newOrigin' => string 'new4' (length=4)
4 =>
array (size=2)
'oldOrigin' => string 'cat5' (length=5)
'newOrigin' => string 'new5' (length=4)
The question is how to replace the origin values in the first array with the newOrigin values, by looking up on the value of oldOrigin.
so the result will be:
$fruits :
array (size=4)
0 =>
array (size=2)
'fruit' => string 'banana' (length=5)
'origin' => string 'new1' (length=4)
1 =>
array (size=2)
'fruit' => string 'orange' (length=6)
'origin' => string 'new2' (length=4)
2 =>
array (size=2)
'fruit' => string 'apple' (length=5)
'origin' => string 'new3' (length=4)
3 =>
array (size=2)
'fruit' => string 'lemon' (length=5)
'origin' => string 'new4' (length=4)
thanks in advance for the help, i am using php 5.
Something like this ?
foreach($fruits as $fruit)
{
foreach($origins as $origin)
{
if($origin['oldOrigin'] == $fruit['origin'])
{
$fruit['origin'] == $origin['newOrigin'];
}
}
}

multidimensional array unset not working

Here is my array;
var_dump($contact['poco']['tags']);
array (size=5)
0 =>
array (size=3)
'tag' => string 'boy' (length=3)
'color' => string '#332409' (length=7)
'id' => string '57160583b0e6df19598b4568' (length=24)
1 =>
array (size=3)
'tag' => string 'girl' (length=4)
'color' => string '#2e2f15' (length=7)
'id' => string '57160589b0e6df1d598b4567' (length=24)
2 =>
array (size=3)
'tag' => string 'zebra' (length=5)
'color' => string '#646604' (length=7)
'id' => string '57160592b0e6df7b588b4567' (length=24)
3 =>
array (size=3)
'tag' => string 'potential duplicate' (length=19)
'color' => string '#f00' (length=4)
'id' => string '57161d9db0e6df0f5c8b456b' (length=24)
4 =>
array (size=3)
'tag' => string 'no phone numbers' (length=16)
'color' => string '#5833d2' (length=7)
'id' => string '5716059ab0e6df7b588b456d' (length=24)
I just want to unset/remove one that have the following tags;
$smartTags = ['potential duplicate', 'no emails', 'no phone numbers'];
So I end up with;
array (size=3)
0 =>
array (size=3)
'tag' => string 'boy' (length=3)
'color' => string '#332409' (length=7)
'id' => string '57160583b0e6df19598b4568' (length=24)
1 =>
array (size=3)
'tag' => string 'girl' (length=4)
'color' => string '#2e2f15' (length=7)
'id' => string '57160589b0e6df1d598b4567' (length=24)
2 =>
array (size=3)
'tag' => string 'zebra' (length=5)
'color' => string '#646604' (length=7)
'id' => string '57160592b0e6df7b588b4567' (length=24)
I have tried;
$smartTags = ['potential duplicate', 'no emails', 'no phone numbers'];
foreach ($contact['poco']['tags'] as $key => $tag) {
if (in_array($tag, $smartTags)) {
unset($contact['poco']['tags'][$key]);
}
}
But it doesn't do anything. I might be having trouble because of the multi-dimensionalness of this array...
What is the correct syntax?
Try up with this.
foreach ($contact['poco']['tags'] as $key => $tag) {
if (in_array($tag['tag'], $smartTags)) {
unset($contact['poco']['tags'][$key]);
}
}

Order by value of array multidimensional

I have array like this
array (size=6)
0 =>
array (size=1)
0 =>
array (size=5)
'document_id' => string '231' (length=3)
'document_title' => string 'Renstra 2014-2018 Distamben COVER' (length=33)
'document_date' => string '2014-10-15 14:09:00' (length=19)
'site' => string 'Propinsi Nusa Tenggara Barat' (length=28)
'url_download' => string 'http://ntbprov.sip-ppid.net/index.php/document/download/231' (length=59)
1 =>
array (size=1)
0 =>
array (size=5)
'document_id' => string '7' (length=1)
'document_title' => string 'Data Menara 2014' (length=16)
'document_date' => string '2015-01-08 13:05:00' (length=19)
'site' => string 'Kabupaten Bima' (length=14)
'url_download' => string 'http://bimakab.sip-ppid.net/index.php/document/download/7' (length=57)
how to sort by document_date? I try with usort() but not working.
I am Newbie.

Merge 1 multidimensional array with a simple array

I am trying to merge 2 arrays: 1 multidimensional and another one normal:
Multidimensional array - $_SESSION["products"]
array (size=2)
0 =>
array (size=4)
'name' => string 'Lg Monitor' (length=10)
'code' => string '30' (length=2)
'qty' => string '1' (length=1)
'price' => string '1300.50' (length=7)
1 =>
array (size=4)
'name' => string 'Smasung Monitor' (length=15)
'code' => string '29' (length=2)
'qty' => string '1' (length=1)
'price' => string '2300.50' (length=7)
Simple array - $qty
array (size=2)
0 => string '2' (length=1)
1 => string '3' (length=1)
EXPECTED OUTPUT
array (size=2)
0 =>
array (size=4)
'name' => string 'Lg Monitor' (length=10)
'code' => string '30' (length=2)
'qty' => string '2' (length=1) // notice the qty change
'price' => string '1300.50' (length=7)
1 =>
array (size=4)
'name' => string 'Smasung Monitor' (length=15)
'code' => string '29' (length=2)
'qty' => string '3' (length=1) // notice the qty change
'price' => string '2300.50' (length=7)
I tried:
foreach ($_SESSION["products"] as $cart_itm){
foreach($qty as $qt) {
$cart_itm['qty'] = $qt;
}
}
But did not work, cart_itm['qty'] remained the same (1).
Try with this:
foreach ($_SESSION["products"] as $key => &$cart_itm){
$cart_itm['qty'] = $qty[$key];
}

Categories