Show match array data between database and array - php

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

Related

error data in loop foreach does'nt display good syntax

I don't understand why in my foreach loop I can't use
$products_option_value['products_option_value_id'] for example. I must use
$option['products_option_value']['products_option_value_id'] to display the good result else I have just a value
Below the elements
var_dump($options_array);
array (size=7)
0 =>
array (size=7)
'products_option_id' => string '213' (length=3)
'products_option_value' =>
array (size=13)
'products_option_value_id' => string '171' (length=3)
'option_value_id' => string '179' (length=3)
'name' => string 'S' (length=1)
'image' => null
'quantity' => string '100' (length=3)
'subtract' => string '0' (length=1)
'price' => string '1.0000' (length=6)
'price_prefix' => string '+' (length=1)
'weight' => string '0.00' (length=4)
'weight_prefix' => string '+' (length=1)
'customers_group_id' => string '99' (length=2)
'products_option_model' => string '99' (length=2)
'option_tax_class_id' => string '99' (length=2)
'option_id' => string '40' (length=2)
'name' => string 'Taille' (length=6)
'type' => string 'select' (length=6)
'value' => null
'required' => null
etc
foreach ($options_array as $option) {
foreach ($option['products_option_value'] as $products_option_value) {
var_dump($products_option_value);
}
}
the result is :
var_dump($option); result
array (size=7)
'products_option_id' => string '213' (length=3)
'products_option_value' =>
array (size=13)
'products_option_value_id' => string '171' (length=3)
'option_value_id' => string '179' (length=3)
'name' => string 'S' (length=1)
'image' => null
'quantity' => string '100' (length=3)
'subtract' => string '0' (length=1)
'price' => string '1.0000' (length=6)
'price_prefix' => string '+' (length=1)
'weight' => string '0.00' (length=4)
'weight_prefix' => string '+' (length=1)
'customers_group_id' => string '99' (length=2)
'products_option_model' => string '99' (length=2)
'option_tax_class_id' => string '99' (length=2)
'option_id' => string '40' (length=2)
'name' => string 'Taille' (length=6)
'type' => string 'select' (length=6)
'value' => null
'required' => null
var_dump($products_option_value) result
products_info_options_new.php:84:string '171' (length=3)
products_info_options_new.php:84:string '179' (length=3)
products_info_options_new.php:84:string 'S' (length=1
etc
http://php.net/manual/en/control-structures.foreach.php
You should use foreach($array as $key => $value) when iterating trough arrays that have keys. (I mean arrays such as this: array("key" => $value), and this is also your array)

push all the records from DB into one hash in PHP

I am trying to push all the records from DB into one hash (associative array) on the basis of uid (as key) below.
Sample dump of Records from DB:
array (size=4)
'id' => string '18' (length=2)
'job_title' => string 'Rubyyyy' (length=4)
'job_skills' => string 'Rubyyyy' (length=4)
'uid' => string '1' (length=1)
array (size=4)
'id' => string '19' (length=2)
'job_title' => string 'Ruby on Rails' (length=13)
'job_skills' => string 'Ruby on Rails' (length=13)
'uid' => string '2' (length=1)
array (size=4)
'id' => string '20' (length=2)
'job_title' => string 'Ruby' (length=4)
'job_skills' => string 'Ruby' (length=4)
'uid' => string '1' (length=1)
array (size=4)
'id' => string '21' (length=2)
'job_title' => string 'Rubyon' (length=13)
'job_skills' => string 'Rubyon' (length=13)
'uid' => string '2' (length=1)
Expected Output:
array (size=1)
1 =>
array (size=4)
'id' => string '20' (length=2)
'job_title' => string 'Ruby' (length=4)
'job_skills' => string 'Ruby' (length=4)
'uid' => string '1' (length=1)
array (size=4)
'id' => string '18' (length=2)
'job_title' => string 'Rubyyyy' (length=4)
'job_skills' => string 'Rubyyyy' (length=4)
'uid' => string '1' (length=1)
2 =>
array (size=4)
'id' => string '21' (length=2)
'job_title' => string 'Rubyon' (length=13)
'job_skills' => string 'Rubyon' (length=13)
'uid' => string '2' (length=1)
array (size=4)
'id' => string '19' (length=2)
'job_title' => string 'Ruby on Rails' (length=13)
'job_skills' => string 'Ruby on Rails' (length=13)
'uid' => string '2' (length=1)

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

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

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