this is my php code.
while($fetchRes=mysql_fetch_assoc($query9)) {
$rows[] = array(
array('iso'=>$staff_id,
'name'=>$name,
'iso3'=>$last_name,
'numcode'=>$email
),);
}
echo '<pre>';
$rows=print_r($rows);
echo '</pre>';
after running this code i am getting the following output.
Array (
[0] => Array (
[0] => Array ( [id] => 008 [name] =>
dev [last_name] => abc [email] => abc#gmail.com ) )
[1] => Array ( [0] => Array ( [name] => 7002
[name] => Sheela [lastname] => Mbhhh [email] => 7002#iem.com ) )
but problem is that i want the following type of array
array(
array('id'=>'008 ',
'name'=>'dev ',
'last_name'=>'abc ',
'email'=>'abc#gmail.com',
),
array('id'=>'AL',
'name'=>'Albania',
'last'=>'ALB',
'email'=>'8',
)
means i dont want the extra things in my array like Array (
[0] =>, Array (
[1] =>
Then why do you use an array inside another array? ($rows[] = array(array(...)
Try this:
while($fetchRes=mysql_fetch_assoc($query9)) {
$rows[] = array(
'iso' => $staff_id,
'name' => $name,
'iso3' => $last_name,
'numcode' => $email
);
}
Related
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 5 months ago.
I need to convert a PHP array that I'm getting from a form submission, so that I can use it more usefully in a db.
Array
(
[first_name] => Array
(
[0] => Ben
[1] => Tom
[2] => Sarah
)
[last_name] => Array
(
[0] => Wills
[1] => Main
[2] => Bliss
)
[email] => Array
(
[0] => ben.wills#argh.com
[1] => tommain#argh.com
[2] => sbliss#argh.com
)
)
to:
Array
(
[0] => Array
(
[first_name] => Ben
[last_name] => Wills
[email] => ben.wills#argh.com
)
[1] => Array
(
[first_name] => Tom
[last_name] => Main
[email] => tommain#argh.com
)
[2] => Array
(
[first_name] => Sarah
[last_name] => Bliss
[email] => sbliss#argh.com
)
)
How can I change the values' key paths so that the first level keys and the second level keys are swapped?
The solution using array_keys, array_values, array_map, call_user_func_array and array_combine functions:
$keys = array_keys($arr); // supposing $arr is your initial array
$data = call_user_func_array("array_map", array_merge([null], array_values($arr)));
$result = array_map(function($v) use($keys){
return array_combine($keys, $v);
}, $data);
print_r($result);
The output:
Array
(
[0] => Array
(
[first_name] => Ben
[last_name] => Wills
[email] => ben.wills#argh.com
)
[1] => Array
(
[first_name] => Tom
[last_name] => Main
[email] => tommain#argh.com
)
[2] => Array
(
[first_name] => Sarah
[last_name] => Bliss
[email] => sbliss#argh.com
)
)
Use the below code. Hope at least this gives some idea how to proceed :)
$array = array(
'first_name' => array('Ben','Tom','Sarah'),
'last_name' => array('Wills','Main','Bliss'),
'email' => array('ben.wills#argh.com','tommain#argh.com','sbliss#argh.com')
);
// loop the array
foreach($array as $key=>$value){
foreach($value as $k=>$v){
// use the first loop key here
$new_array[$k][$key] = $v;
}
}
print_r($new_array);
Out Put:
Array
(
[0] => Array
(
[first_name] => Ben
[last_name] => Wills
[email] => ben.wills#argh.com
)
[1] => Array
(
[first_name] => Tom
[last_name] => Main
[email] => tommain#argh.com
)
[2] => Array
(
[first_name] => Sarah
[last_name] => Bliss
[email] => sbliss#argh.com
)
)
Doing a transform while retaining the key names can be achieved quite easily using PHP's MultipleIterator
$data = array(
'first_name' => array(
0 => 'Ben',
1 => 'Tom',
2 => 'Sarah',
),
'last_name' => array(
0 => 'Wills',
1 => 'Main',
2 => 'Bliss',
),
'email' => array(
0 => 'ben.wills#argh.com',
1 => 'tommain#argh.com',
2 => 'sbliss#argh.com',
),
);
$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_ASSOC);
foreach($data as $key => $column) {
$mi->attachIterator(new ArrayIterator($column), $key);
}
$newData = [];
foreach($mi as $row) {
$newData[] = $row;
}
var_dump($newData);
Demo
I've an array titled $val. It's a blank array. I want it in following manner i.e. after print_r($val); it should print the array $val in following manner:
I've following variables too :
$_POST['fileName'] = 'Sample_1.docx';
$_POST['fileLink'] = 'https://www.filepicker.io/api/file/HEYQ6lPNRg2lzjUNhUGX';
Array
(
[status_info] =>
[user_status] => Share what's going on...
[group_id] =>
[action] => upload_photo_via_share
[is_activity_feed] => 1
[vshare] => Array
(
[Sample_1.docx] => Array
(
[0] => https://www.filepicker.io/api/file/HEYQ6lPNRg2lzjUNhUGX
)
)
[link] => Array
(
[url] => http://
)
[poll_question] =>
[answer] => Array
(
[0] => Array
(
[answer] =>
)
[1] => Array
(
[answer] =>
)
)
[is_profile] => no
[iframe] => 1
[method] => simple
[document_title] => document
[category] => Array
(
[0] =>
)
[code_snippet] =>
)
I want to add following structure to the variable $val. This thing should be done using PHP code only.
How should I add following structure of key-values to the array $val in PHP?
Thanks in advance.
The PHP source of this array should be like this:
$val = array(
'status_info' => '',
'user_status' => 'Share what's going on...',
...
'vshare' => array(
'Sample_1.docx' => array (
'https://www.filepicker.io/api/file/HEYQ6lPNRg2lzjUNhUGX'
)
),
'link' => array(
'url' => 'http://'
),
...
);
Is it possible to remove only the associative array who have all values empty?
Data source:
Array
(
[0] => Array
(
[name] => foo
[phone] => 012345
[email] =>
)
[1] => Array
(
[name] => bar
[phone] =>
[email] => yahoo.com
)
[2] => Array
(
[name] =>
[phone] =>
[email] =>
)
)
Desired output:
Array
(
[0] => Array
(
[name] => foo
[phone] => 012345
[email] =>
)
[1] => Array
(
[name] => bar
[phone] =>
[email] => yahoo.com
)
)
I tried this, but unfortunately I will delete all empty values of arrays
$_arr = array_filter(array_map('array_filter', $_arr));
Array
(
[0] => Array
(
[name] => foo
[phone] => 012345
)
[1] => Array
(
[name] => bar
[email] => yahoo.com
)
)
How could I do it? Thank You
Maybe a slicker way, but:
$array = array_filter($array, function($a) { return array_filter($a); });
Since array_filter is using a true or false return to filter; the array_filter in the function is returning either an empty array evaluated as false, or a non-empty array evaluated as true, and the main array_filter is filtering based upon that.
<?php
$collection = array(
"0" => array
(
'name' => "foo",
'phone' => "012345",
'email' => ''
),
"1" => array
(
'name' => "bar",
'phone' => '',
'email' => "yahoo.com",
),
"2" => array
(
'name' => '',
'phone' => '',
'email' => ''
)
);
foreach($collection as $key=> $entry){
if(count(array_filter($entry)) == 0){
unset($collection[$key]);
}
}
print_r($collection);
I am using this to create an array:
foreach($results as $tire){
$group_price[] = array($tire->group, $tire->price);
}
My results are:
Array (
[0] => Array (
[0] => MAXAT
[1] => 118.91
)
[1] => Array (
[0] => FZSUV
[1] => 137.81
)
[2] => Array (
[0] => MAXAT
[1] => 153.79
)
)
What I would like my results to be:
Array (
[0] => Array (
[group] => MAXAT
[price] => 118.91
)
[1] => Array (
[group] => FZSUV
[price] => 137.81
)
[2] => Array (
[group] => MAXAT
[price] => 153.79
)
)
I am just not sure how to change my foreach in a way that would change the output.
Like so:
foreach($results as $tire){
$group_price[] = array(
'group' => $tire->group,
'price' => $tire->price
);
}
Try:
foreach($results as $tire){
$group_price[] = array('group' => $tire->group,'price' => $tire->price);
}
Just like so:
foreach($results as $tire){
$group_price[] = array(
'group' => $tire->group,
'price' => $tire->price
);
}
Add keys to your array elements, that's all.
foreach($results as $tire){
$group_price[] = array('group'=>$tire->group, 'price'=>$tire->price);
}
Try
array('group' => $tire->group, 'price' => $tire->price);
I'm trying to put data into from:
[comments] => Array
(
[count] => 2
[data] => Array
(
[0] => Array
(
[idcomments] => 1
[from] => Array
(
[idusers] => 1
[username] =>
[full_name] => AndrewLiu
)
[text] => testing this comment out
)
[1] => Array
(
[idcomments] => 2
[from] => Array
(
[idusers] => 1
[username] =>
[full_name] => AndrewLiu
)
[text] => more comments yeah
)
)
)
I have something like this:
while($row = $SQL_products -> fetch(PDO::FETCH_ASSOC)){
$json['data'][] = array(
"comments" =>array(
"count"=>$SQL_ccount[0],
"data" => array($SQL_comments->fetchAll(PDO::FETCH_ASSOC),
"from" => array($SQL_comments_from->fetchAll(PDO::FETCH_ASSOC)))
),
);
}
But it doesn't fall into the from. Not sure what the right syntax is to get $SQL_comments_from into data such like the example above.
This is what I'm getting
[comments] => Array
(
[count] => 2
[data] => Array
(
[0] => Array
(
[0] => Array
(
[idcomments] => 1
[from] => 1
[text] => testing this comment out
)
[1] => Array
(
[idcomments] => 2
[from] => 2
[text] => more comments yeah
)
)
[from] => Array
(
[0] => Array
(
[idusers] => 1
[username] =>
[full_name] => AndrewLiu
)
)
)
)
I'm trying to get "from" into the "data". The example I provided does not make the "from" go into the other "from" (thats right under idcomments)
Thanks in advance!
Your data field uses data from SQL_comments_from and $SQL_comments_from mixed together, this should generate what you want.
while($row = $SQL_products -> fetch(PDO::FETCH_ASSOC)){
$from = $SQL_comments_from->fetchAll(PDO::FETCH_ASSOC);
$data= array();
foreach ($SQL_comments->fetchAll(PDO::FETCH_ASSOC as $items){
$data[] = array('idcomments' => $items['idcomments'], 'from' => $from[$items['from']], 'text' => $items['text']);
}
$json['comments'][] = array(
"count"=>$SQL_ccount[0],
"data" => $data;
)
),
);
}