Cakephp getting all data form left side table - php

I am using following binding
$this->FranchiseZip->bindModel(array(
'belongsTo'=>array(
'Franchise',
),
),false
);
it will return result in below format
Array
(
[0] => Array
(
[FranchiseZip] => Array
(
[id] => 1
)
[Franchise] => Array
(
[name] => abc
)
)
but i have many records in FranchiseZip model where Franchise id is null. it will not return those rows. Please tell me how i fetch those rows in cakephp. i want result in following format
Array
(
[0] => Array
(
[FranchiseZip] => Array
(
[id] => 1
)
[Franchise] => Array
(
[name] => abc
)
)
[1 => Array
(
[FranchiseZip] => Array
(
[id] => 123
)
[Franchise] => Array
(
[name] =>
)
)
thanks

Related

How to Fetch data from Array in WordPress

Array
(
[0] => stdClass Object
(
[meta_id] => 23233
[post_id] => 4467
[meta_key] => first_name
[meta_value] => Daud
)
)
How can I echo post_id from this array for all posts using while or foreach statement?
Array
(
[classic-editor-remember] => Array
(
[0] => classic-editor
)
[_edit_lock] => Array
(
[0] => 1582905950:5
)
[_edit_last] => Array
(
[0] => 5
)
[_thumbnail_id] => Array
(
[0] => 4376
)
[slide_template] => Array
(
[0] => default
)
[_yoast_wpseo_content_score] => Array
(
[0] => 30
)
[_yoast_wpseo_primary_advisor_category] => Array
(
[0] =>
)
[title] => Array
(
[0] => Demo Daniel Wrenne, CFP, ChFC
)
[designation] => Array
(
[0] => Wrenne Financial Planing, LLC Lexington, KY
)
[client_specialities] => Array
(
[0] => Gen Y/Millennials, Medical Professionals
)
[address] => Array
(
[0] => 3223 S LEHI DR
)
[phone_number] => Array
(
[0] => 64646446486
)
[email_address] => Array
(
[0] => demo#demo.com
)
[website_url] => Array
(
[0] => a:3:{s:3:"url";s:23:"https://www.google.com/";s:4:"text";s:20:"View Advisor Profile";s:6:"target";s:4:"none";}
)
[first_name] => Array
(
[0] => Daud
)
[last_name] => Array
(
[0] => Yahya
)
)
And how can I get las_name, first_name, email, address, website url, specialities, designation and title from the above array using and loop like while or foreach loop.
This is less a WordPress question and a basic PHP foreach question.
The first example you have is an Object, so you need to access the properties, e.g. meta_id, post_id like:
// THIS IS JUST AN EXAMPLE. YOUR VARIABLE WILL CHANGE BASED ON HOW YOU GOT THE DATA. `$object_array` is how you got the data to begin with.
foreach( $object_array as $object ) {
$post_id = $object->post_id;
echo $post_id;
}
For your second example, since there is only one array key inside each array key, you would set it up like this:
// Example. you would use whatever you used to get the array to begin with as the `$array`.
foreach ($array as $item ) {
$last_name = $item['last_name'][0];
$first_name = $item['first_name'][0];
....
}

how to remove primary key from result set in laravel mongodb?

I have following query
$tag_query = UserTags::query();
$tag_query->whereIn('tag_id' , $insert_data['to_device']);
$tag_users = $tag_query->get(['user_id']);
it is generating following result -
Array
(
[0] => Array
(
[_id] => 57da358e7ac6f6740e8b456a
[user_id] => 57d67290823fb647dd174739
)
[1] => Array
(
[_id] => 57da358e7ac6f6740e8b456c
[user_id] => 57d672cb823fb647dd17473a
)
[2] => Array
(
[_id] => 57da358e7ac6f6740e8b4571
[user_id] => 57d67549d81e1845e4dba983
)
)
I don't want _id in result array , how can I remove it without using loop?
expected output -
Array
(
[0] => Array
(
[user_id] => 57d67290823fb647dd174739
)
[1] => Array
(
[user_id] => 57d672cb823fb647dd17473a
)
[2] => Array
(
[user_id] => 57d67549d81e1845e4dba983
)
)
please help! Thanks!
You can apply projections to your queries using the project() method:
$tag_query = UserTags::query();
$tag_query->whereIn('tag_id' , $insert_data['to_device']);
$tag_users = $tag_query->project(['_id' => 0, 'user_id' => 1])->get();

Insert values inside multidimensional array

In my PHP application, get the results from DB. After processing the results I need to convert the results like below using foreach
Array
(
[1] => Array -----> This is intent 1, this key indicates all intent values which is equal to 1, should belongs to here.
(
[0] => Array
(
[name] => A
[indent] => 1
)
[1] => Array
(
[name] => B
[indent] => 1
)
)
[2] => Array
(
[0] => Array
(
[name] => B
[indent] => 2
)
[1] => Array
(
[name] => A
[indent] => 2
)
)
[3] => Array
(
[0] => Array
(
[name] => A
[indent] => 3
)
)
)
That I have some intent value common, common intent values are stored in array like array('1'=> array(array[0],array[1]));.
What I tried is
foreach($results as $data){
$root_array[$data['intent']] = array($data);
}
This will replace the old array and insert the last intent value which is common.
I get result like below, the intent 1 and intent 2 are replaced with last data
Array
(
[1] => Array
(
[0] => Array
(
[name] => B
[indent] => 1
)
)
[2] => Array
(
[0] => Array
(
[name] => A
[indent] => 2
)
)
[3] => Array
(
[0] => Array
(
[name] => A
[indent] => 3
)
)
)
In the loop you must check if the current indent has been initialized. If not then create it, else just append the new data to it.
foreach($results as $data) {
if (!isset($root_array[$data['indent']])) {
$root_array[$data['indent']] = array($data);
} else {
$root_array[$data['indent']][] = $data;
}
}

Read data from a single record OR more records

I have a query which gives data from one person or from more persons.
The data for one person is presented with JSON as:
Array ( [0] => Array (
[a2a_Person] => Array (
[pid] => Person1
[a2a_PersonName] => Array (
[a2a_PersonNameFirstName] => Array ( [a2a_PersonNameFirstName] => Aagje )
[a2a_PersonNameLastName] => Array ( [a2a_PersonNameLastName] => Baltus ) ) ....
If there are more persons then the data is presented as
Array ( [0] => Array (
[a2a_Person] => Array (
[0] => Array (
[pid] => Person1 [a2a_PersonName] => Array (
[a2a_PersonNameFirstName] => Array ( [a2a_PersonNameFirstName] => Walig )
[a2a_PersonNamePatronym] => Array ( )
[a2a_PersonNamePrefixLastName] => Array ( )
[a2a_PersonNameLastName] => Array ( [a2a_PersonNameLastName] => Verdugt ) ) [a2a_Gender] => Array ( [a2a_Gender] => Man )
[1] => Array (
[pid] => Person2
[a2a_PersonName] => Array (
[a2a_PersonNameFirstName] => Array ( [a2a_PersonNameFirstName] => Huibert ) [a2a_PersonNamePatronym.....
My question is now : how can i determine if I have more Person-records so i can acces the data correct way.
And what is the best way to acces the data. Must I first check the definition and write different code.
Thanks,
Fred

How can I sort through multidimensional arrays within multidimensional arrays to eliminate repetitions?

Here is an example output from when I print out its contents:
Array
(
[0] => Array
(
[CountryA] => Array
(
[ProvinceA] => Array
(
[CityA] => Array
(
[SuburbA] =>
)
)
)
)
[1] => Array
(
[CountryA] => Array
(
[ProvinceA] => Array
(
[CityA] => Array
(
[SuburbB] =>
)
)
)
)
[2] => Array
(
[CountryA] => Array
(
[ProvinceB] => Array
(
[CityB] => Array
(
[SuburbC] =>
)
)
)
)
[3] => Array
(
[CountryB] => Array
(
[ProvinceD] => Array
(
[CityE] => Array
(
[SuburbE] =>
)
)
)
)
What I would like to do is create a function that parses it in some way (and perhaps creates a new array) so that the result will look something like:
Array
(
[0] => Array
(
[CountryA] => Array
(
[ProvinceA] => Array
(
[CityA] => Array
(
[SuburbA] =>
[SuburbB} =>
)
)
[ProvinceB] =>
(
[CityB] => Array
(
[SuburbC] =>
)
)
)
)
[1] => Array
(
[CountryB] => Array
(
[ProvinceD] => Array
(
[CityE] => Array
(
[SuburbE] =>
)
)
)
)
Thanks in advance!!
Change you structure, your array should not look like this :
Array(
[0] => Array([Country A] => data),
[1] => Array([Country B] => data)
)
But more like this :
Array(
[Country A] => data,
[Country B] => data
)
Once you've done this, it will be trivial to add a city in your array :
If the country exists in the array, add to the country, else add it to the array and stop
[Add to country :] If the province exists in the country, add to the province, else add the province to the array and stop
Same for city, suburb... you get the idea

Categories