convert cavtivedataprovider to array with specific form - php

im using yii1 on my application.
i want to convert from cActivedataProvider to array
this is the code
$dataSS = new CActiveDataProvider('category', array(
'criteria' => array(
'condition' => 'menu=:menu',
'params' => array(':menu' => $menu),
),
'pagination' => false
));
$dataMenu = array();
foreach ($dataSS->getData() as $record) {
$dataMenu[] = array(
'label' => $record->name,
'url' => '#',
);
}
this is the result :
Array (
[0] => Array ( [label] => Food and Drink [url] => # )
[1] => Array ( [label] => Sleman [url] => # )
)
the result that i expected :
Array (
Array ( 'label' => 'Food and Drink', 'url' => '#' ) ,
Array ( 'label' => 'Sleman', 'url' => '#' ) ,
)
any suggestion?

Finally get the answer,
this is really my bad because i call the function on wrong way.
This is the wrong way :
'items' =>array(Category::model()->getMenu("2");),
and this is the correct way :
'items' =>Category::model()->getMenu("2"),

Related

How to change array format in PHP?

I have simple question:
How to change this array
Array ( [0] => taxonomy-term-1 [1] => taxonomy-term-2 [2] => taxonomy-term-3 )
to this array?
array( 'taxonomy-term-1', 'taxonomy-term-2', 'taxonomy-term-3' )
Why I am asking for that:
$slugs = wp_get_post_terms($post->ID,'ml_patternsCustomTaxonomy',['fields'=>'slugs']);
returns
Array ( [0] => taxonomy-term-1 [1] => taxonomy-term-2 [2] => taxonomy-term-3 )
but
array(
'title' => $title,
'content' => trim($patternContent),
'categories' => $slugs
)
doesn't work, and
//
'categories' => array( 'taxonomy-term-1', 'taxonomy-term-2', 'taxonomy-term-3' ),
works.
Why?

How can I avoid that array_merge_recursive() tranform properties into arrays?

I'm trying to array_merge_recursive two arrays.
$arrayA = array(
'properties' => array(
'path' => array(
'default' => '',
),
'multiple' => array(
'default' => true,
),
)
);
$arrayB = array(
'properties' => array(
'path' => array(
'default' => 'foo',
),
'multiple' => array(
'default' => false,
),
)
);
$array = array_merge_recursive($arrayA,$arrayB);
print_r($array);
Which gives
Array (
[properties] => Array (
[path] => Array (
[default] => Array (
[0] =>
[1] => foo
)
)
[multiple] => Array (
[default] => Array (
[0] => 1
[1] =>
)
)
)
)
As you can see, the default and multiple properties are merged into arrays since it exists in both arrays.
But I don't want them to be transformed into arrays, I want that the last value to override the previous one.
And of course they DO exists in both array, I cannot declare it only in the second one.
How could I achieve this ?
Thanks !

md5 php array value

I need to hash a value using md5 in a PHP array value,
'password' => array(
'value' => md5($form_data['password'])
)
I tried with the above code but it didn't seem to work.
check this code and working . i think your code is correct . so if you show your full array then we can understand which section problem. i define array as your code array and it is working fine
<?php
$array = array(
'password' => array(
'value' => '12456'
),
'name' => array(
'name' => 'Test Name'
)
);
echo "<pre>";
print_r($array);
$md5_array = array(
'password' => array(
'value' => md5('12456')
),
'name' => array(
'name' => 'Test Name'
)
);
echo "<pre>";
print_r($md5_array);
?>
then output before md5 :
Array
(
[password] => Array
(
[value] => 12456
)
[name] => Array
(
[name] => Test Name
)
)
after md5 :
Array
(
[password] => Array
(
[value] => 6a9edcb7b63821802aa44d35d531c9fc
)
[name] => Array
(
[name] => Test Name
)
)
for more information
http://php.net/manual/en/function.md5.php

How to get distance with $near?

I can get my elements, with the following code :
$lines = $loc->find(
array("loc" =>
array('$near' =>
array('$geometry' =>
array('type' => 'Point', 'coordinates' =>
array(floatval($longitude), floatval($latitude))
),
'$maxDistance' => 10000 //meters
)
)
)
);
This is the results :
Array
(
[_id] => MongoId Object
(
[$id] => 5490003c815289663d8bbd95
)
[name] => My adress
[loc] => Array
(
[type] => Point
[coordinates] => Array
(
[0] => 5.050948
[1] => 45.040419
)
)
)
But now, I need to get the distance from my given point.
Is it possible ?
EDIT :
This is the code used to insert datas :
$loc = $client->localisation->localisation;
$loc->ensureIndex( array("loc" => "2dsphere"));
$loc->insert(
array(
"idobject" => $myValue
"name" => $myName
"loc" => array("type" => "Point", "coordinates" => array($longitude, $latitude))
));
The $near operator does not return a distance along with the returned results. It only orders the documents in the response.
Under the PHP driver the easiest form is to use the $geoNear aggregation pipeline stage instead. This allows you to project a field in the results, or even use it in other pipeline stages:
$loc->aggregate(
array(
array(
'$geoNear' => array(
'near' => array(
'type' => 'Point',
'coordinates' =>array(floatval($longitude), floatval($latitude))
),
'maxDistance' => 1000,
'distanceField' => 'distance',
'spherical' => true
)
)
)
);
There is is a database command form for geoNear as well, but direct command results are often not as nice.

Removing unnecessary nested index in array (generated by Cake PHP)

The following question can either be solved by probably changing my use of the find method in Cake PHP OR using some PHP function. I would prefer to solve it using Cake but it doesn't matter too much. I have any array like this:
Array
(
[0] => Array
(
[Model] => Array
(
[id] => 14
[foo] => bar
)
)
[1] => Array
(
[Model] => Array
(
[id] => 15
[foo] => something
)
) .............
I just want to remove the Model index and just use the numeric one. The following function generated this array:
$arr = $this->Model->find('all', array('contain' => false ) );
I probably need to change the 'contain' part of the call. Basically, in addition to the data that appears under each Model index, I also have a second and third model and the contain = false just restricts Cake from getting data from the current model (Model).
If I understand your question correctly, I think CakePHP's Set::combine function will help http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::combine :
$result = Set::combine($your_array, '{n}.Model.id', '{n}.Model.data');
This will result in:
Array
(
[14] => Array
(
[foo] => bar
)
[15] => Array
(
[foo] => something
)
)
you have to write your own piece of code to modify your array , here is a function that will do what you want (you can improve it)
function reformArray($array,$modelName)
{
foreach($arr as $key => $value)
{
$newArr[] = $arr[$key][$modelName];
}
return $newArr;
}
you have to pass your array and the Model Name to this function and will return you the result
$a = array(
array(
'User' => array(
'id' => 2,
'group_id' => 1,
'Data' => array(
'user' => 'mariano.iglesias',
'name' => 'Mariano Iglesias'
)
)
),
array(
'User' => array(
'id' => 14,
'group_id' => 2,
'Data' => array(
'user' => 'phpnut',
'name' => 'Larry E. Masters'
)
)
),
array(
'User' => array(
'id' => 25,
'group_id' => 1,
'Data' => array(
'user' => 'gwoo',
'name' => 'The Gwoo'
)
)
)
);
RUN THIS TO REMOVE THE MODEL NAME USER
Set::extract($a, '{n}.User');
WILL RETURN THIS
$a = array(
array(
'id' => 2,
'group_id' => 1,
'Data' => array(
'user' => 'mariano.iglesias',
'name' => 'Mariano Iglesias'
)
),
array(
'id' => 14,
'group_id' => 2,
'Data' => array(
'user' => 'phpnut',
'name' => 'Larry E. Masters'
)
),
array(
'id' => 25,
'group_id' => 1,
'Data' => array(
'user' => 'gwoo',
'name' => 'The Gwoo'
)
)
);
In CakePHP 2:
$data = $this->Model->find('all');
$data = Set::extract('/Model/.', $data );
You can achieve this result by foreach also
foreach ($data as $k => &$t) {
$t = $t['Model']
}
It will be much easier with Object. Change the Array with Object.
Actually I had faced the same problem with nested array And I found the solution with Set::map($array);
If you don't want the model_name as nested array, You can prefer this solution.
$data = array(
array(
"IndexedPage" => array(
"id" => 1,
"url" => 'http://blah.com/',
'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
'get_vars' => '',
'redirect' => '',
'created' => "1195055503",
'updated' => "1195055503",
)
),
array(
"IndexedPage" => array(
"id" => 2,
"url" => 'http://blah.com/',
'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
'get_vars' => '',
'redirect' => '',
'created' => "1195055503",
'updated' => "1195055503",
),
)
);
$mapped = Set::map($data);
/* $mapped now looks like: */
Array
(
[0] => stdClass Object
(
[_name_] => IndexedPage
[id] => 1
[url] => http://blah.com/
[hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
[get_vars] =>
[redirect] =>
[created] => 1195055503
[updated] => 1195055503
)
[1] => stdClass Object
(
[_name_] => IndexedPage
[id] => 2
[url] => http://blah.com/
[hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
[get_vars] =>
[redirect] =>
[created] => 1195055503
[updated] => 1195055503
)
)
you may do this :
$tmp = $this->Model->find('all', array('contain' => false ) );
$arr = $tmp['ModelName'];

Categories