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]
);
}
Related
How to get 'syn' and 'sim' values as a string from the given arrays, as array could vary i only want to extract 'syn' and 'sim' as an example following arrays are given ,i know it might be a simple question but i am new to multidimensional array that's why can't seem to solve it.
array (size=1)
'adjective' =>
array (size=2)
'syn' =>
array (size=7)
0 => string 'cagey' (length=5)
1 => string 'cagy' (length=4)
2 => string 'canny' (length=5)
3 => string 'apt' (length=3)
4 => string 'cunning' (length=7)
5 => string 'ingenious' (length=9)
6 => string 'adroit' (length=6)
'sim' =>
array (size=4)
0 => string 'adroit' (length=6)
1 => string 'artful' (length=6)
2 => string 'intelligent' (length=11)
3 => string 'smar
array (size=2)
'noun' =>
array (size=3)
'syn' =>
array (size=23)
0 => string 'passion' (length=7)
1 => string 'beloved' (length=7)
2 => string 'dear' (length=4)
3 => string 'dearest' (length=7)
4 => string 'honey' (length=5)
5 => string 'sexual love' (length=11)
6 => string 'erotic love' (length=11)
7 => string 'lovemaking' (length=10)
8 => string 'making love' (length=11)
9 => string 'love life' (length=9)
10 => string 'concupiscence' (length=13)
11 => string 'emotion' (length=7)
12 => string 'eros' (length=4)
13 => string 'loved one' (length=9)
14 => string 'lover' (length=5)
15 => string 'object' (length=6)
16 => string 'physical attraction' (length=19)
17 => string 'score' (length=5)
18 => string 'sex' (length=3)
19 => string 'sex activity' (length=12)
20 => string 'sexual activity' (length=15)
21 => string 'sexual desire' (length=13)
22 => string 'sexual practice' (length=15)
'ant' =>
array (size=1)
0 => string 'hate' (length=4)
'usr' =>
array (size=1)
0 => string 'amour' (length=5)
'verb' =>
array (size=2)
'syn' =>
array (size=29)
0 => string 'love' (length=4)
1 => string 'enjoy' (length=5)
2 => string 'roll in the hay' (length=15)
3 => string 'make out' (length=8)
4 => string 'make love' (length=9)
5 => string 'sleep with' (length=10)
6 => string 'get laid' (length=8)
7 => string 'have sex' (length=8)
8 => string 'know' (length=4)
9 => string 'do it' (length=5)
10 => string 'be intimate' (length=11)
11 => string 'have intercourse' (length=16)
12 => string 'have it away' (length=12)
13 => string 'have it off' (length=11)
14 => string 'screw' (length=5)
15 => string 'jazz' (length=4)
16 => string 'eff' (length=3)
17 => string 'hump' (length=4)
18 => string 'lie with' (length=8)
19 => string 'bed' (length=3)
20 => string 'have a go at it' (length=15)
21 => string 'bang' (length=4)
22 => string 'get it on' (length=9)
23 => string 'bonk' (length=4)
24 => string 'copulate' (length=8)
25 => string 'couple' (length=6)
26 => string 'like' (length=4)
27 => string 'mate' (length=4)
28 => string 'pair' (length=4)
'ant' =>
array (size=1)
0 => string 'hate' (length=4)
Any help would be appreciated thanks!
Create a function:
function getArrayAsString($array, $key) {
if (isset($array[$key])) {
return is_array($array[$key]) ? implode(', ', $array[$key]) : $array[$key];
}
return '';
}
Usage example:
$synData = getArrayAsString($array['adjective'], 'sim');
$simData = getArrayAsString($array['noun'], 'syn');
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)
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);
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]);
}
}
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