Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #4646

Re: Fast/Easy way to extract a column from multi-dimensional array?

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <1574609.VNBv5SFTvI@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <php@PointedEars.de>
Organization PointedEars Software (PES)
Date Fri, 27 Jan 2012 22:42:33 +0100
User-Agent KNode/4.4.11
Content-Transfer-Encoding 8Bit
Subject Re: Fast/Easy way to extract a column from multi-dimensional array?
Newsgroups comp.lang.php
References <jfv06v01hoa@news6.newsguy.com> <jfv2pk$aot$1@news.albasani.net>
Followup-To comp.lang.php
MIME-Version 1.0
Lines 73
NNTP-Posting-Date 27 Jan 2012 22:42:33 CET
NNTP-Posting-Host e692efc3.newsspool3.arcor-online.net
X-Trace DXC=SA?^CL0o?gMWDmlTRbh@=IMcF=Q^Z^V3H4Fo<]lROoRA8kF<OcfhCOKa`:6Ko^Zg`LDZm8W4\YJNL;?f@h5gMfbLGaaV0`_>WCAf6<BMk8A3GE
X-Complaints-To usenet-abuse@arcor.de
Xref x330-a1.tempe.blueboxinc.net comp.lang.php:4646

Followups directed to: comp.lang.php

Show key headers only | View raw


Thomas Mlynarczyk wrote:

> John Drako schrieb:
>> I'm looking for a way to get all the values from the column of a two
>> dimensional array.
> 
>> RecordID, name, last name, title
>> RecordID, name, last name, title
>> RecordID, name, last name, title
>> RecordID, name, last name, title
>> RecordID, name, last name, title
>> RecordID, name, last name, title
>> etc...
>> 
>> I need to get all the 'RecordID' from the results to reuse in other
>> queries.
> 
> If it's the first column you're interested in:
> 
> array_map( 'current', $array )

array_map() it is, but one should not rely on that the array cursor is at 
the first element.  Since the OP is passing MYSQLI_ASSOC, they should use

  $recordIds = array_map(
    create_function('$e', 'return $e["RecordID"]'),
    $arr
  );

for one-time use or

  public static function getRecordId($result)
  {
    return $result['RecordID'];
  }

  public function getResults()
  {
    $arr = …;

    $recordIds = array_map(array('self', 'getRecordId'), $arr);
  }

in a class.  Adjust visibility to the necessary minimum.

However, it is better to map the result of a database query to model objects 
(cf. MVC pattern):

  public function getResults()
  {
    $arr = …;

    foreach ($arr as $row)
    {
      $results[] = new Result($row);
    }

    return $results;
  }

The (example) `Result' constructor would then cause the elements of the 
array to be mapped, according to their key, to properties of `Result' 
instances, allowing for implicit range checking and type conversion through 
setters (preferably the __set() magic method calling them on access of non-
public properties from public context).


PointedEars
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Fast/Easy way to extract a column from multi-dimensional array? John Drako <jbravo556@gmail.removethis.com> - 2012-01-27 15:08 -0500
  Re: Fast/Easy way to extract a column from multi-dimensional array? "J.O. Aho" <user@example.net> - 2012-01-27 21:50 +0100
    Re: Fast/Easy way to extract a column from multi-dimensional array? "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-01-27 22:49 +0100
  Re: Fast/Easy way to extract a column from multi-dimensional array? Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2012-01-27 21:52 +0100
    Re: Fast/Easy way to extract a column from multi-dimensional array? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-27 22:42 +0100
      Re: Fast/Easy way to extract a column from multi-dimensional array? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-27 22:55 +0100
      Re: Fast/Easy way to extract a column from multi-dimensional array? Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2012-01-27 23:49 +0100
  Re: Fast/Easy way to extract a column from multi-dimensional array? Jeff North <jnorthau@yahoo.com.au> - 2012-01-28 13:52 +1100
    Re: Fast/Easy way to extract a column from multi-dimensional array? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-28 21:28 +0100
  Re: Fast/Easy way to extract a column from multi-dimensional array? Arno Welzel <usenet@arnowelzel.de> - 2012-01-28 09:49 +0100
    Re: Fast/Easy way to extract a column from multi-dimensional array? John Drako <jbravo556@gmail.removethis.com> - 2012-01-29 13:54 -0500

csiph-web