multidimensional array unset not working - php

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]);
}
}

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.

Show match array data between database and array

I have a problem.
This data on my database table of column name "XYZ"
string 'aaa' (length=3)
string 'bbb' (length=3)
string 'ccc' (length=3)
and this is api array (fetch data). Now I want which "property_name" match in my database column "XYZ" show only this array. How can I do that?
array (size=12)
'property_code' => string 'YXDUB006' (length=8)
'property_name' => string 'bbb' (length=19)
'address' =>
array (size=4)
'line1' => string 'Jessop Street' (length=13)
'city' => string 'County Laois' (length=12)
'postal_code' => string 'R32 RV20' (length=8)
'country' => string 'IE' (length=2)
'contacts' =>
array (size=2)
0 =>
array (size=2)
'type' => string 'PHONE' (length=5)
'detail' => string '353-578-678588' (length=14)
1 =>
array (size=2)
'type' => string 'FAX' (length=3)
'detail' => string '353-57-8678577' (length=14)
array (size=13)
'property_code' => string 'YXDUB006' (length=8)
'property_name' => string 'aaa' (length=19)
'address' =>
array (size=4)
'line1' => string 'Jessop Street' (length=13)
'city' => string 'County Laois' (length=12)
'postal_code' => string 'R32 RV20' (length=8)
'country' => string 'IE' (length=2)
'contacts' =>
array (size=2)
0 =>
array (size=2)
'type' => string 'PHONE' (length=5)
'detail' => string '353-578-678588' (length=14)
1 =>
array (size=2)
'type' => string 'FAX' (length=3)
'detail' => string '353-57-8678577' (length=14)
array (size=14)
'property_code' => string 'YXDUB006' (length=8)
'property_name' => string 'ggg' (length=19)
'address' =>
array (size=4)
'line1' => string 'Jessop Street' (length=13)
'city' => string 'County Laois' (length=12)
'postal_code' => string 'R32 RV20' (length=8)
'country' => string 'IE' (length=2)
'contacts' =>
array (size=2)
0 =>
array (size=2)
'type' => string 'PHONE' (length=5)
'detail' => string '353-578-678588' (length=14)
1 =>
array (size=2)
'type' => string 'FAX' (length=3)
'detail' => string '353-57-8678577' (length=14)
You need to do it like below:-
$final_array = array();
$sql="select hotel FROM hotels";
$result=$mysqli->query($sql);
while ($myrow = $result->fetch_array(MYSQLI_ASSOC)){
foreach($api_array as &$value){
if($myrow['hotel'] == $value['property_name']){
$final_array[] = $value;
}
}
}
echo "<pre/>";print_r($final_array);

Loop through multi-dimensional array and remove duplicate and merge other values

I have array like below. I want to unset all array having same q_id but merge its name key.
array
0 =>
array
'field_name' => string 'Hindu rastrya' (length=13)
'name' => string '283' (length=3)
'q_id' => string '199' (length=3)
1 =>
array
'field_name' => string 'dharma nirpachya' (length=16)
'name' => string '284' (length=3)
'q_id' => string '199' (length=3)
2 =>
array
'field_name' => string 'j vaye pni hunxa' (length=16)
'name' => string '285' (length=3)
'q_id' => string '199' (length=3)
3 =>
array
'field_name' => string 'Nepal' (length=5)
'name' => string '286' (length=3)
'q_id' => string '200' (length=3)
4 =>
array
'field_name' => string 'India' (length=5)
'name' => string '287' (length=3)
'q_id' => string '200' (length=3)
5 =>
array
'field_name' => string 'China' (length=5)
'name' => string '288' (length=3)
'q_id' => string '200' (length=3)
Expected:
array
0 =>
array
'field_name' => string 'Hindu rastrya' (length=13)
'name' =>
array
0 => string '283' (length=3)
1 => string '284' (length=3)
2 => string '285' (length=3)
'q_id' => string '199' (length=3)
1 =>
array
'field_name' => string 'Nepal' (length=16)
'name' =>
array
0 => string '286' (length=3)
1 => string '287' (length=3)
2 => string '288' (length=3)
'q_id' => string '200' (length=3)
I tried following code but It didnot preserve the keys so it fails:
foreach ($form_option_data as $key=>$option)
{
if($form_option_data[$key]['q_id'] == $form_option_data[$key+1]['q_id']){
unset($form_option_data[$key+1]);
}
}
The following should do the job:
$data = ...; //initial array
$result = array();
foreach ($data as $entry) {
if (!array_key_exists($entry['q_id'], $result)) {
$result[$entry['q_id']] = array(
'q_id' => $entry['q_id'],
'name' => array($entry['name'])
);
} else {
$result[$entry['q_id']]['name'][] = $entry['name'];
}
}
$result = array_values($result); // to re-index the table so that keys start from 0

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];
}

How to parse multi-line CSV into multidimensional associative array?

Content of $csv_content is:
1415797658,456ABC,789,123,"bla"
1415797656,654XYZ,897,567,"foo"
1415797654,639HJW,465,146,"bar"
str_getcsv(file_get_contents($csv_content)) results in:
array
0 => string '1415797658' (length=10)
1 => string '456ABC' (length=6)
2 => string '789' (length=3)
3 => string '123' (length=3)
4 => string 'bla' (length=3)
5 => string '1415797656' (length=10)
6 => string '654XYZ' (length=6)
7 => string '897' (length=3)
8 => string '567' (length=3)
9 => string 'foo' (length=3)
10 => string '1415797654' (length=10)
11 => string '639HJW' (length=6)
12 => string '465' (length=3)
13 => string '146' (length=3)
14 => string 'bar' (length=3)
Desired result:
array
0 =>
array
'timestamp' => string '1415797658' (length=10)
'id' => string '456ABC' (length=6)
'id2' => string '789' (length=3)
'id3' => string '123' (length=3)
'text' => string 'bla' (length=3)
1 =>
array
'timestamp' => string '1415797656' (length=10)
'id' => string '654XYZ' (length=6)
'id2' => string '897' (length=3)
'id3' => string '567' (length=3)
'text' => string 'foo' (length=3)
2 =>
array
'timestamp' => string '1415797654' (length=10)
'id' => string '639HJW' (length=6)
'id2' => string '465' (length=3)
'id2' => string '146' (length=3)
'text' => string 'bar' (length=3)
What would be the neatest way to do this?
$array=array();
$handle=fopen($csv_content,'r');
while ($row=fgetcsv($handle)){
$array[]=array(
'timestamp' => $row[0],
'id' => $row[1],
'id2' => $row[2],
'id3' => $row[3],
'text' => $row[4]
);
}

Categories