Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Trying to build the multidimensional array with a foreach using record from a db...
$servDetails = array(
// CSS Servers
'css' => array(
'server1' => array(
'id' => 'id1',
'type' => 'css',
'host' => 'ip:port'
),
'server2' => array(
'id' => 'id2',
'type' => 'css',
'host' => 'ip:port'
)
)
);
What's the best way of doing this?
Assuming you have executed and have populated an associative array:
// create your containing array
$servDetails = array('css' => array());
// iterate over the results
foreach ($results as $result) {
$servDetails['css'][$result['server_name']] = array(
'id' => $result['server_id'],
'type' => 'css',
'host' => $result['server_ip'] . ':' . $result['server_port']
);
}
I'm not sure where the 'css' portion comes from in your sample, so you may have to adjust this to make it dynamic (if it is in fact dynamic).
You could also build this array structure directly when pulling the results from the db:
if ($results = $mysqli->query($query)) {
while ($result = $results->fetch_assoc()) {
$servDetails['css'][$result['server_name']] = array(
'id' => $result['server_id'],
'type' => 'css',
'host' => $result['server_ip'] . ':' . $result['server_port']
);
}
Related
I am using phpdocx to generate an array with my data in a docx format.
$contact is an array of multiple object. Sometimes $contact contain 1 object, and sometimes more.
I want to make a loop, to add contact as much as I need.
My problem : For exemple, If I am doing this I will get an error like "Undefined array key 3" if my contact data only contain 3 object or less.
important : Here, if my datas contain 4 object (from 0 to 3 ) it will work but doesn't work when i have 2 objects.
$contact= array(
array(
'name' => $request->get('contact')[0]['name'],
'userName' => $request->get('contact')[0]['userName'],
'number' => $request->get('contact')[0]['number'],
'mail' => $request->get('contact')[0]['mail'],
),
array(
'name' => $request->get('contact')[1]['name'],
'userName' => $request->get('contact')[1]['userName'],
'number' => $request->get('contact')[1]['number'],
'mail' => $request->get('contact')[1]['mail'],
),
array(
'name' => $request->get('contact')[2]['name'],
'userName' => $request->get('contact')[2]['userName'],
'number' => $request->get('contact')[2]['number'],
'mail' => $request->get('contact')[2]['mail'],
),
array(
'name' => $request->get('contact')[3]['name'],
'userName' => $request->get('contact')[3]['userName'],
'number' => $request->get('contact')[3]['number'],
'mail' => $request->get('contact')[3]['mail'],
),
);
$docx->replaceTableVariable($contact, array('parseLineBreaks' => true));
what i am actually trying with no success for the moment : https://www.phpdocx.com/en/forum/default/topic/1773
I have make a loop and it is now working
$contactData = $request->get('contact');
$contactTab = array();
for ($i = 0; $i < count($contactData); $i++) {
$rowData = array(
'name' => $contactData[$i]['name'],
'userName' => $contactData[$i]['userName'],
'number' => $contactData[$i]['number'],
'mail' => $contactData[$i]['mail'],
);
$contactTab[] = $rowData;
}
$docx->replaceTableVariable($contactTab, array('parseLineBreaks' => true));
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to create a sitemap for a website that is coded in zf1 but I don't know anything about this.
I've read on google that I need a controller, a view and routes for it but what I found is for zf2 and doesn't works.
Did someone makes this ?
You should use the Zend_Navigation class and navigation sitemap view helper inside a Controller Action, as follows:
public function sitemapAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$items = array(
array(
'title' => 'Title 1',
'label' => 'Label 1',
'uri' => 'https://www.example.com/page1',
'order' => 1,
'type' => 'uri',
'changefreq' => 'weekly',
),
array(
'title' => 'Title 2',
'label' => 'Label 2',
'uri' => 'https://www.example.com/page2',
'order' => 2,
'type' => 'uri',
'changefreq' => 'weekly',
)
);
$nav = new Zend_Navigation($items);
$sitemapOutput = $this->view->navigation($nav)->sitemap();
$this->_response->setHeader('Content-Type', 'text/xml; charset=utf-8')->setBody($sitemapOutput);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Is there any built in function to enable the lookup function in PHP like the excel one? I thought the array_search would be the closest one, but it won't work if the mixed needle(value) has more than one values. To illustrate, the lookup I try to perform is:
function lookup($datatype){
$array = array(
'BINARY' => 'RAW',
'REAL' => 'FLOAT',
'INTEGER' => 'NUMBER',
'DATETIME' => 'DATE',
'VARCHAR' => 'VARCHAR2',
'DATETIME' => 'TIMESTAMP',
'VARBINARY' => 'BFILE',
'INT' => 'NUMERIC'
);
$key = array_search($datatype, $array);
return $key;
}
If the lookup is for the 'RAW', array_search will return 'BINARY', but when the lookup is 'DATE', it won't return anything.
Any thoughts would be much appreciated.
the $array has the same key 'DATETIME' twice ,second one will overrite the first, so $array don't have the 'DATE' key
You can't have the same key twice in your array! You can instead of 'DATETIME' to 'DATETIME_test'
function lookup($datatype){
$array = array(
'BINARY' => 'RAW',
'REAL' => 'FLOAT',
'INTEGER' => 'NUMBER',
'DATETIME_test' => 'DATE',
'VARCHAR' => 'VARCHAR2',
'DATETIME' => 'TIMESTAMP',
'VARBINARY' => 'BFILE',
'INT' => 'NUMERIC',
);
$key = array_search($datatype, $array);
return $key;
}
var_dump(lookup('DATE'));
As you mentioned your array, there is duplicate key of "DATETIME", so it will overwrite by last value. and your array would be as follow.
$array = array(
'BINARY' => 'RAW',
'REAL' => 'FLOAT',
'INTEGER' => 'NUMBER',
'VARCHAR' => 'VARCHAR2',
'DATETIME' => 'TIMESTAMP',
'VARBINARY' => 'BFILE',
'INT' => 'NUMERIC'
);
so whenever you are trying to filter out or willing to get, only one key is available.
Better to write down with different key. and then try to array_search for key.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I used the cakephp console to generate some CRUD operations. The generated code was buggy as the model of an associated model was not loaded by default in the controller.
For instance:
$programs = $this->Chantier->Program->find('list');
would not work, but:
$this->loadModel('Program');
$programs = $this->Program->find('list');
would.
Here is the code of the associations:
/**
* belongsTo associations
*
* #var array
*/
public $belongsTo = array(
'Programs' => array(
'className' => 'Programs',
'foreignKey' => 'Programs_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Etats' => array(
'className' => 'Etats',
'foreignKey' => 'Etats_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Types' => array(
'className' => 'Types',
'foreignKey' => 'Types_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'ChampsLibres' => array(
'className' => 'ChampsLibres',
'foreignKey' => 'Champs_libres_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
Take a close look to your code:
public $belongsTo = array(
'Programs' => array(
'className' => 'Programs',
'foreignKey' => 'Programs_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
Both the alias for the association and classname are in plural, while this is possible and fine, it goes against CakePHP conventions. You are trying this:
$programs = $this->Chantier->Program->find('list');
But note that you wrote Program in singular and not in plural as declared in your associations. I would suggest to re-bake your code to follow convention to avoid future headaches and confusion.
I always have trouble wrapping my head around these foreach array things, and SO has been an incredibly value resource in getting me there, so I hope you guys can help with this one.
public function progress_bar()
{
$items = array(
array(
'step-name' => 'Setup',
'url' => '/projects/new/setup/',
),
array(
'step-name' => 'Artwork',
'url' => '/projects/new/artwork/',
),
array(
'step-name' => 'Review',
'url' => '/projects/new/review/',
),
array(
'step-name' => 'Shipping',
'url' => '/projects/new/shipping-info/',
),
array(
'step-name' => 'Billing',
'url' => '/projects/new/billing/',
),
array(
'step-name' => 'Receipt',
'url' => '/projects/new/receipt/',
),
);
// Status can be active, editing, or complete.
foreach ($this->progress as $step => $status)
{
foreach ($items as $item)
{
$item['step-name'] == ucfirst($step) ? $item['status'] = $status : '';
}
}
return $items;
}
$this->progress contains an array of statuses ('setup' => 'active', 'artwork' => 'editing')
I want to add to the $items array the status of each matching item in $this->progress
$items = array(
array(
'step-name' => 'Setup',
'url' => '/projects/new/setup',
'status' => 'active',
),
etc...
);
If I understand your question correctly, the problem is that you are trying to add an array element to $items, but what you're actually doing is adding the element to a temporary variable ($item), which does not reference the original $items variable.
I'd suggest approaching it like this:
foreach ($this->progress as $step => $status)
{
// Having the $key here allows us to reference the
// original $items variable.
foreach ($items as $key => $item)
{
if ($item['step-name'] == ucfirst($step) )
{
$items[$key]['status'] = $status;
}
}
}
return $items;
Are you locked into using an array to store $items? If so, you're going to be stuck doing a nested loop ("For each element in $this->progress, check each element in $items. If match, update $items" or something like that). If you have some flexibility, I would use a hash for $items (associative array in php-speak), where the index is the step name. So $items['Setup'] would contain 'url' => ... and 'status' => ... etc. Does that make sense? Then your algorithm breaks down to "For each element in $this->progress, get the element in $items by name ($items[$step_name]) and update it's info."
I would change how you have the $items array keyed and do it like this. Stops you from having the nested loops.
public function progress_bar()
{
$items = array(
'Setup' => array(
'url' => '/projects/new/setup/',
),
'Artwork' => array(
'url' => '/projects/new/artwork/',
),
'Review' => array(
'url' => '/projects/new/review/',
),
'Shipping' => array(
'url' => '/projects/new/shipping-info/',
),
'Billing' => array(
'url' => '/projects/new/billing/',
),
'Receipt' => array(
'url' => '/projects/new/receipt/',
)
);
// Status can be active, editing, or complete.
foreach ($this->progress as $step => $status)
{
$item[ucfirst($step)]['status'] = $status;
}
return $items;
}