jQuery order json list from php? - php

i am sending an ordered json_encode list of some MySQL tables, from php, but when i retrieve it with jquery it is not in order any more? everything works fine and in order on the php side. it's the client side that i'm having trouble with. i think the problem is that i'm sending a multidimensional array from php as json. what would be the most efficient solution? also why has the order changed when retrieved by jQuery.
PHP CODE:
$user_data = array();
while($row = mysql_fetch_array($retval, MYSQL_ASSOC){
$user_id = $row['user_id'];
if(!isset($user_data[$user_id]){
$user_data[$user_id] = array(
'first_name' => $row['first_name'],
'last_name' => $row['last_name'],
'dept' => $row['dept'],
'quals' => array()
);
}
$quals = array(
'qual_cat' => $row['qual_cat'],
'qual' => $row['qual'],
'count' => $row['count']
)
$user_data[$user_id]['quals'][] = $quals;
}
echo json_encode($user_data);
jQuery:
$.post('page.php', function(post){
$.each(post, function(i,data){
alert(data.first_name+' '+data.last_name+' - '+data.dept);
});
});
PHP VAR_DUMP:
array
10 =>
array
'first_name' => string 'David' (length=5)
'last_name' => string 'Dan' (length=3)
'dept' => string 'web' (length=3)
'count' => string '5' (length=1)
'quals' =>
array
0 =>
array
...
1 =>
array
...
2 =>
array
...
3 =>
array
...
4 =>
array
...

In php, array is by default associative, so that's why you have this behavior as associative array order is not guaranteed (as per explanation in the link given by benedict_w).
To overcome this, you could try the following:
echo json_encode(array_values($user_data));
This will effectively turn your json from
["10":{prop1:val1, prop2:val2}, "25":{prop1:val1, prop2:val2}]
into
[{prop1:val1, prop2:val2}, {prop1:val1, prop2:val2}]
If you need to keep track of the id, put it inside your user_data in your php:
if(!isset($user_data[$user_id]){
$user_data[$user_id] = array(
'id' => $user_id,
'first_name' => $row['first_name'],
'last_name' => $row['last_name'],
'dept' => $row['dept'],
'quals' => array()
);
}

Related

CakePHP: Load name field of related model on view

Guys I have a simple but boring question...
I have the following scenario:
Solicitation -> HasMany -> Destiny
Destiny -> BelongsTo -> City
My problem is on the view, I want to retrieve the City name instead of the city_id and I can't figure out.
Here is my Result:
array (size=12)
'Solicitation' =>
array (size=32)
'id' => string '25' (length=2)
'Destiny' =>
array (size=2)
0 =>
array (size=3)
'id' => string '3' (length=1)
'solicitation_id' => string '25' (length=2)
'city_id' => string '4382' (length=4)
1 =>
array (size=3)
'id' => string '4' (length=1)
'solicitation_id' => string '25' (length=2)
'city_id' => string '4350' (length=4)
How can I retrieve the city name on the view?
Here's my controller:
public function mySolicitations()
{
$this->Solicitation->recursive = 1;
$person_id = $this->Auth->user('id');
$conditions = array(
'Solicitation.created_by' => $person_id
);
$this->set(array(
'data' => $this->paginate('Solicitation', $conditions, array('Solicitation.id' => 'DESC')),
'title_for_layout' => 'My Solicitations'
));
}
Is there a problem with my relation?
Thanks in advance.
There are two ways:
Change your model recursive value to 2.
$this->Solicitation->recursive = 2;
The drawback of using this is when you have quite a few associations involved, it'll retrieve a lot of unwanted unnecessary data.
You can use "Containable" Behavior here.
$this->Solicitation->Behaviors->load("Containable");
And specify the model names that you want to retrieve:
$this->Solicitation->contain(array(
"Destiny", "Destiny.City"
)
);
Then add your code:
$conditions = array(
'Solicitation.created_by' => $person_id
);
$this->set(array(
'data' => $this->paginate('Solicitation', $conditions, array('Solicitation.id' => 'DESC')),
'title_for_layout' => 'My Solicitations'
));
If you debug, you'll certainly find the "City" index within each Destiny sub arrays.
Hope this helps.
Peace! xD

How to get id as key and email as its value from multidimensional array?

I have multi-dimensional array like below
$records = array(
array(
'id' => 11,
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'john#gmail.com'
),
array(
'id' => 12,
'first_name' => 'Sally',
'last_name' => 'Smith',
'email' => 'sally#gmail.com'
),
array(
'id' => 13,
'first_name' => 'Jane',
'last_name' => 'Jones',
'email' => 'jane#gmail.com'
)
);
Now,I want the output as array in form of id as key and email as its value.
Array
(
[11] => john#gmail.com
[12] => sally#gmail.com
[13] => jane#gmail.com
)
I want short code for it, Not want any lengthy code.
I have tried it with foreach
$collect_data = array();
foreach($records as $key=>$data){
$collect_data[$data['id']] = $data['email']
}
Any one know short code for above to achieve my requirement.
I think, You can try php in-build function to sort out your solution
$emails = array_column($records, 'email', 'id');
print_r($last_names);
You can also refer following link too.
Php In-build Function (array_column)
This should work for you:
Just array_combine() the id column with the email column, which you can grab with array_column().
$collect_data = array_combine(array_column($records, "id"), array_column($records, "email"));
For compatibility sake, as array_column was introduced in PHP 5.5, consider array_reduce:
$output = array_reduce($records, function($memo, $item){
$memo[$item['id'] = $item['email'];
return $memo;
}, []);
I find it more expressive than a foreach, but that's matter of personal taste.

Transpose an array of arrays [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 6 months ago.
Hah, I had no idea how else to phrase that. I'm trying to reformat a set of three arrays generated by form field inputs, into something that better matches my models, so I can save the values to the db.
Not sure if the solution should be some array manipulation or that I should change the "name" attribute in my form fields.
currently I have an array of my input data:
array(
'image_id' =>
array
0 => '454' (length=3),
1 => '455' (length=3),
2 => '456' (length=3)
'title' =>
array
0 => 'title1' (length=6),
1 => 'title2' (length=0),
2 => '' (length=6)
'caption' =>
array
0 => 'caption1' (length=8),
1 => '' (length=8),
2 => 'caption3' (length=8)
);
and would like to change it to something like, so I can iterate over and save each array of values to the corresponding resource in my db.
array(
0 =>
array
'image_id' => '454',
'title' => 'title1',
'caption' => 'caption1'
1 =>
array
'image_id' => '455',
'title' => 'title2',
'caption' => ''
2 =>
array
'image_id' => '456',
'title' => '',
'caption' => 'caption3'
);
This would iterate through the array with 2 foreach loops. They would use each other's key to construct the new array, so it would work in any case:
$data = array(
'image_id' => array(454, 455, 456),
'title' => array('title1', 'title2', ''),
'caption' => array('caption1', '', 'caption3')
);
$result = array();
foreach($data as $key => $value) {
foreach ($value as $k => $v) {
$result[$k][$key] = $v;
}
}
This'll do it:
$array = call_user_func_array('array_map', array_merge(
[function () use ($array) { return array_combine(array_keys($array), func_get_args()); }],
$array
));
Assuming though that this data is originally coming from an HTML form, you can fix the data right there already:
<input name="data[0][image_id]">
<input name="data[0][title]">
<input name="data[0][caption]">
<input name="data[1][image_id]">
<input name="data[1][title]">
<input name="data[1][caption]">
Then it will get to your server in the correct format already.

Doctrine hydrated array result

I want to get Doctrine to return a hydrated array with the values being the id for the key, then all values inside an array of results (i.e. if there are multiple items with same ID, then return ID with multiple results in array).
This is the current function I do:
public static function getMedia($em, $entity, $id = NULL)
{
$dql = 'SELECT m.id, m.url, m.nb, m.lang
FROM iMT\Entity\Media m INDEX BY m.id JOIN iMT\Entity\\' . $entity . ' r WITH m.id = r.id';
if($id) {
$dql .= " WHERE r.id = ?1";
}
$q = $em->createQuery($dql);
if($id) {
$q->setParameter(1, $id);
}
return $q->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
}
Which returns:
array (size=44)
479600 =>
array (size=4)
'id' => int 479600
'url' => string 'pois/479600/Nonna.JPG' (length=48)
'nb' => null
'lang' => string 'fr' (length=2)
479615 =>
array (size=4)
'id' => int 479615
'url' => string 'pois/479615/Tramways.jpg' (length=51)
'nb' => null
'lang' => string 'fr' (length=2)
479580 =>
array (size=4)
'id' => int 479580
'url' => string 'pois/479580/ATLAS.jpg' (length=48)
'nb' => null
'lang' => string 'fr' (length=2)
479581 =>
array (size=4)
'id' => int 479581
'url' => string 'pois/479581/P'tit_sushi.jpg' (length=54)
'nb' => null
'lang' => string 'fr' (length=2)
However, I need the output to be:
array (size=44)
479600 =>
array (size=2)
array (size=4)
'id' => int 479600
'url' => string 'pois/479600/Nonna.JPG' (length=48)
'nb' => null
'lang' => string 'fr' (length=2)
array (size=4)
'id' => int 479600
'url' => string 'pois/479600/OtherPic.JPG' (length=48)
'nb' => null
'lang' => string 'fr' (length=2)
Would I need to create my own AbstractQuery::HYDRATE_ARRAY or is there something available that does what I need?
I'm using the result by checking if it contains a key that matches the ID of the current item (e.g. if(isset($data[$item])) // where $item = 479600 then output images), maybe there's a better way to check for the results?
EDIT
I've updated my function to return:
$result = $q->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
$data = array();
$count = count($result);
for($i = 0; $i < $count; $i++) {
if(!isset($data[$result[$i]['id']])) {
$data[$result[$i]['id']] = array(
$result[$i]
);
} else {
$data[$result[$i]['id']][] = $result[$i];
}
}
return $data;
Which returns something more to what I want:
array (size=44)
479600 =>
array (size=1)
0 =>
array (size=4)
'id' => int 479600
'url' => string 'pois/479600/Nonna.JPG' (length=48)
'nb' => null
'lang' => string 'fr' (length=2)
479577 =>
array (size=2)
0 =>
array (size=4)
'id' => int 479577
'url' => string 'pois/479577/AOMC.JPG' (length=47)
'nb' => null
'lang' => string 'fr' (length=2)
1 =>
array (size=4)
'id' => int 479577
'url' => string 'pois/479577/Buffet AOMC.jpg' (length=54)
'nb' => null
'lang' => string 'fr' (length=2)
Can this be improved? Is there any Doctrine functions that can help, or should I leave my for() loop?
The problem with using INDEX BY together with a JOIN is that the result that doctrine gives you might not contain all data that's fetched from the database.
In your case the database might return multiple rows containing the same value for m.id (because of the JOIN). But each subsequent row containing the same value for m.id will overwrite the previous one (because of the INDEX BY m.id).
Doctrine does not come with a hydrator that can solve this problem out of the box. You shall indeed need to implement your own. Read more about creating custom hydration modes.
Alternative
Another solution would be to not use INDEX BY in this case.
You could write a repository method that translates the result given by Doctrine to the array you want to have. Other parts of your application can then call that repository method.
This is probably easier than creating a custom hydration mode.
Update
The translation can look like this:
$data = array();
foreach ($q->getArrayResult() as $row) {
if (!isset($data[$row['id']])) {
$data[$row['id']] = array();
}
$data[$row['id']][] = $row;
}
return $data;

How to turn sql result to multi-dimensional array dynamically?

Here is the query string.
$query = "SELECT t.id, t.assignee, t.owner,
d.code, d.status, d.target_completion_date,
d.target_extension_date, d.submission_date, d.approval_date,
d.revision_start_date, d.revision_completion_date, d.message,
ty.name, f.orig_name, f.new_name,
b.payment_date, b.discount, b.total_cost, b.amount_payed, b.edit_level,
b.billing_type, b.pages, b.words
FROM tasks t
INNER JOIN details d ON t.detail_id = d.id
INNER JOIN billing b ON t.billing_id = b.id
INNER JOIN TYPE ty ON d.document_type_id = ty.id
INNER JOIN files f ON t.file_id = f.id
WHERE t.assignee = 'argie1234'";
And this is the array i would like the query result to turn into.
$user = array('allTask'=>array(array('taskid' => 1,
'assignee'=>'argie1234',
'owner'=>'austral1000',
'details' => array( 'code' => 'E',
'status'=>'TC',
'targetCompletionDateUTC'=>'1379401200',
'targetExtentionDateUTC'=>'1379401200',
'submissionDateUTC'=>'1379401200',
'approvalDateUTC'=>'1379401200',
'revisionStartDateUTC'=>'1379401200',
'revisionCompletionDateUTC'=>'1379401200',
'messageToEditor'=>'Please work on it asap.',
'documentType' => 'Thesis'),
'file' => array('orig_name' =>'originalname.docx',
'new_name' => 'newname.docx'),
'billing'=>array('paymentDate'=>'July 26,2013 12:40',
'discount' => '0',
'totalRevisionCharge' => '$20.00',
'totalAmountPayed' => '$20.00',
'revisionLevel' => '1',
'chargeType'=> '1',
'numPages' => '60',
'numWords' => '120,000' ) ),
array('taskid' => 12,
'assignee'=>'argie1234',
'owner'=>'usaroberto',
'details' => array( 'code' => 'E',
'status'=>'TC',
'targetCompletionDateUTC'=>'1379401200',
'targetExtentionDateUTC'=>'1379401200',
'submissionDateUTC'=>'1379401200',
'approvalDateUTC'=>'1379401200',
'revisionStartDateUTC'=>'1379401200',
'revisionCompletionDateUTC'=>'1379401200',
'messageToEditor'=>'Please work on it asap.',
'documentType' => 'Thesis'),
'file' => array('orig_name' => 'originalname.docx',
'new_name' => 'newname.docx'),
'billing'=>array('paymentDate'=>'July 26,2013 12:40',
'discount' => '0',
'totalRevisionCharge' => '$20.00',
'totalAmountPayed' => '$20.00',
'revisionLevel' => '1',
'chargeType'=> '1',
'numPages' => '60',
'numWords' => '120,000' ) ),
'account' => array( 'username' => 'marooon55',
'emailadd' => 'marooon#yahoo.com',
'firstname' => 'Maroon',
'initial' => 'E',
'lastname' => 'Young',
'country' => 'Australia',
'gender' => 'M',
'password' =>'360e2801190744a2af74ef6cbfdb963078b59709',
'activationDate' => '2013-09-13 14:30:34') );
How can i create the above array? I sure know how to define multi dimensional array, regretfully though i am having difficulty creating this complex array dynamically. As a beginner i don't even know where to begin.
Here is an example that might help you out. Try starting with simple multi dimensional arrays, once you get a hold of it, you can move onto building complex ones. You will then find that the array you want to build is not really difficult than you initially thought it to be.
$mycomplexarray = array('key1' => array('val1', 'val2'),
'key2' => array('val3', 'val4' => array('val5', 'val6')
)
);
You could create the array just as you have here. I'm not gonna write the whole thing out, but something like this...
$result = $mysqli->query($query); // however you query the db is up to you.
$row = $result->fetch_assoc(); //same as query use your prefered method to fetch
$user = array('allTask'=>array(array('taskid' => $row['id'],
'assignee'=>$row['assignee'],
'owner'=>$row['owner'],
'details' => array( 'code' => $row['code'],
'status'=>$row['status'],
...etc, Hope this makes sense for you.
Set up a structure array first that defines which columns will be stored in a sub array like
$struc=array('Id'->0, 'assignee'->0, 'owner'->0,
'code'->'detail', 'status'->'detail', 'target_completion_date'->'detail',
'target_extension_date'->'detail', 'submission_date'->'detail', 'approval_date'->'detail',
'revision_start_date'->'detail', 'revision_completion_date'->'detail', 'message'->'detail',
'name'->'file', 'orig_name'->'file', 'new_name'->'file',
'payment_date'->'billing', 'discount'->'billing', 'total_cost'->'billing', 'amount_payed'->'billing', 'edit_level'->'billing', 'billing_type'->'billing', 'words');
In your while ($a=mysqli_fetch_assoc($res)) loop you can now use this structure to decide whether you want to store an element directly in your target array or whether you want to place it in the subarray named in this structure array. Like
$res=mysqli_query($con,$sql);
$arr=array();
while($a=mysqli_fetch_assoc($res)) {
// within result loop: $a is result from mysqli_fetch_assoc()
$ta=array(); // temp array ...
foreach ($a as $k => $v){
if ($struc[$k]) $ta[struc[$k]][$k]=$v;
else $ta[$k]=$v;
}
$arr[]=$ta; // add to target array
}
This is the complete code, no more is needed. It was typed up on my iPod, so it is NOT tested yet.
The generated array should be equivalent to your $user['allTask'] array.

Categories